Fix: suppress Foo[...] suggestion when annotation uses keyword args#21197
Open
vpawela wants to merge 3 commits intopython:masterfrom
Open
Fix: suppress Foo[...] suggestion when annotation uses keyword args#21197vpawela wants to merge 3 commits intopython:masterfrom
vpawela wants to merge 3 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
Author
|
Updated the fix after mypy_primer flagged regressions in ibis. The condition was too strict; len(e.args) == 1 was incorrectly suppressing valid suggestions for zero-arg and multi-arg positional calls. The fix now only suppresses the suggestion when keyword arguments are present, which is the only case where Foo[...] is genuinely misleading. |
This comment has been minimized.
This comment has been minimized.
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: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(...)
|
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.
Fixes #16506
Problem
When a type annotation uses a call expression with keyword arguments (e.g.
x: Foo(arg=1)), mypy correctly raises a[valid-type]error but also appends:This suggestion is misleading because
Foo[arg=1]is not valid Python syntax. The suggestion only makes sense for single positional-arg calls likeFoo(int), which could plausibly have been meant asFoo[int].Fix
In
mypy/fastparse.py, the suggestion invisit_Callis now only emitted when the call has exactly one positional argument and no keyword arguments.Testing
test-data/unit/check-fastparse.testcheck-fastparse.testcases continue to passFoo(int)style annotationsNote: I'm aware of the suggestion in the issue thread to add more context-specific notes. This PR takes the minimal approach of suppressing the incorrect suggestion; a follow-up could add more targeted messaging.