Fix: suppress Foo[...] suggestion when annotation uses keyword args by vpawela · Pull Request #21197 · python/mypy
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:
note: Suggestion: use Foo[...] instead of Foo(...)
This suggestion is misleading because Foo[arg=1] is not valid Python syntax. The suggestion only makes sense for single positional-arg calls like Foo(int), which could plausibly have been meant as Foo[int].
Fix
In mypy/fastparse.py, the suggestion in visit_Call is now only emitted when the call has exactly one positional argument and no keyword arguments.
Testing
- Added a regression test to
test-data/unit/check-fastparse.test - All 33 existing
check-fastparse.testcases continue to pass - Verified that the suggestion still correctly appears for
Foo(int)style annotations
Note: 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.