Commit 4f94903
authored
Don't suggest Foo[...] when Foo(arg=...) is used in annotation (#21238)
## Summary
Fixes #16506
When a function call with keyword arguments appears in a type annotation
(e.g., `Foo(sort=True)`), mypy was suggesting `use Foo[...] instead of
Foo(...)`. This suggestion is misleading because `Foo[sort=True]` is a
syntax error -- the user likely intended a function call (e.g.,
returning `Annotated[...]`), not a type parameterization.
This PR changes the behavior so that:
- When keyword arguments are present (e.g., `Foo(sort=True)`), mypy
shows `Cannot use a function call in a type annotation` instead of the
misleading `Foo[...]` suggestion.
- When only positional arguments are used (e.g., `Foo(int)`), the
existing `Suggestion: use Foo[...] instead of Foo(...)` message is
preserved, since the user likely meant `Foo[int]`.
## Changes
- `mypy/fastparse.py`: In `visit_Call`, check `e.keywords` before
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
- [x] Existing test `testFasterParseTypeErrorCustom_no_native_parse`
still passes (positional args get `Foo[...]` suggestion)
- [x] New test `testFasterParseCallWithKeywordArgs_no_native_parse`
passes (keyword args get "Cannot use a function call" message)
- [x] All 39 fastparse-related tests pass
- [x] Manual verification with reproduction script from the issue1 parent ba4f018 commit 4f94903
2 files changed
+15
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1958 | 1958 | | |
1959 | 1959 | | |
1960 | 1960 | | |
1961 | | - | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
1962 | 1965 | | |
1963 | 1966 | | |
1964 | 1967 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
210 | 221 | | |
211 | 222 | | |
212 | 223 | | |
| |||
0 commit comments