Don't suggest Foo[...] when Foo(arg=...) is used in annotation#21238
Open
yosofbadr wants to merge 1 commit intopython:masterfrom
Open
Don't suggest Foo[...] when Foo(arg=...) is used in annotation#21238yosofbadr wants to merge 1 commit intopython:masterfrom
yosofbadr wants to merge 1 commit intopython:masterfrom
Conversation
When a function call with keyword arguments appears in a type annotation (e.g. `Foo(sort=True)`), mypy was suggesting to use `Foo[...]` instead. This suggestion is misleading because `Foo[sort=True]` is a syntax error -- the user likely intended a function call, not a type parameterization. Now, when keyword arguments are present, mypy shows "Cannot use a function call in a type annotation" instead. The `Foo[...]` suggestion is preserved for calls with only positional arguments (e.g. `Foo(int)`) where the user likely meant `Foo[int]`. Fixes python#16506
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: ibis (https://github.com/ibis-project/ibis)
- ibis/backends/datafusion/udfs.py:22: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
+ ibis/backends/datafusion/udfs.py:22: note: Cannot use a function call in a type annotation
- ibis/backends/datafusion/udfs.py:30: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
+ ibis/backends/datafusion/udfs.py:30: note: Cannot use a function call in a type annotation
- ibis/backends/datafusion/udfs.py:42: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
+ ibis/backends/datafusion/udfs.py:42: note: Cannot use a function call in a type annotation
- ibis/backends/datafusion/udfs.py:50: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
+ ibis/backends/datafusion/udfs.py:50: note: Cannot use a function call in a type annotation
- ibis/backends/datafusion/udfs.py:111: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
+ ibis/backends/datafusion/udfs.py:111: note: Cannot use a function call in a type annotation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #16506
When a function call with keyword arguments appears in a type annotation (e.g.,
Foo(sort=True)), mypy was suggestinguse Foo[...] instead of Foo(...). This suggestion is misleading becauseFoo[sort=True]is a syntax error -- the user likely intended a function call (e.g., returningAnnotated[...]), not a type parameterization.This PR changes the behavior so that:
Foo(sort=True)), mypy showsCannot use a function call in a type annotationinstead of the misleadingFoo[...]suggestion.Foo(int)), the existingSuggestion: use Foo[...] instead of Foo(...)message is preserved, since the user likely meantFoo[int].Changes
mypy/fastparse.py: Invisit_Call, checke.keywordsbefore choosing which note to attach. If the call has keyword arguments, use a generic "cannot call" message instead of suggesting bracket syntax.test-data/unit/check-fastparse.test: Added a test case for the keyword argument scenario.Test plan
testFasterParseTypeErrorCustom_no_native_parsestill passes (positional args getFoo[...]suggestion)testFasterParseCallWithKeywordArgs_no_native_parsepasses (keyword args get "Cannot use a function call" message)