Suppress misleading Foo[...] suggestion for annotations with keyword args#21179
Suppress misleading Foo[...] suggestion for annotations with keyword args#21179Chessing234 wants to merge 1 commit intopython:masterfrom
Conversation
When a call expression with keyword arguments is used in a type annotation (e.g., `Foo(arg=val)`), mypy previously suggested using `Foo[...]` instead. However, `Foo[arg=val]` is a SyntaxError in Python, making the suggestion misleading. This change suppresses the bracket syntax suggestion when the call contains keyword arguments. Fixes python#16506 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
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:30: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
- ibis/backends/datafusion/udfs.py:42: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
- ibis/backends/datafusion/udfs.py:50: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
- ibis/backends/datafusion/udfs.py:111: note: Suggestion: use dt.Timestamp[...] instead of dt.Timestamp(...)
|
|
Your two tests seems redundant with each other 🤷♂️ |
|
Repeating my suggestion from the main issue discussion, since it's also relevant here:
|
Summary
Foo(arg=val)), mypy previously suggesteduse Foo[...] instead of Foo(...). SinceFoo[arg=val]is aSyntaxErrorin Python, this suggestion is misleading.Changes
mypy/fastparse.py: Added a check fore.keywordsinvisit_Callso theSuggestion: use Foo[...] instead of Foo(...)note is only shown when there are no keyword arguments.test-data/unit/check-fastparse.test: Added test casetestFasterParseTypeErrorCustomWithKeywordverifying no suggestion note is emitted forFoo(arg=int).test-data/unit/check-errorcodes.test: Added test casetestErrorCodeSyntaxError4verifying the error code behavior with keyword arguments.Test plan
testFasterParseTypeErrorCustom_no_native_parsestill passes (existing behavior preserved for positional args)testFasterParseTypeErrorCustomWithKeyword_no_native_parsepasses (new test for keyword args)testErrorCodeSyntaxError4_no_native_parsepasses (new error code test)testErrorCodeSyntaxError*tests pass🤖 Generated with Claude Code