Skip to content

Fix: suppress Foo[...] suggestion when annotation uses keyword args#21197

Open
vpawela wants to merge 3 commits intopython:masterfrom
vpawela:fix/16506-no-foo-bracket-suggestion
Open

Fix: suppress Foo[...] suggestion when annotation uses keyword args#21197
vpawela wants to merge 3 commits intopython:masterfrom
vpawela:fix/16506-no-foo-bracket-suggestion

Conversation

@vpawela
Copy link
Copy Markdown

@vpawela vpawela commented Apr 9, 2026

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.test cases 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.

@github-actions

This comment has been minimized.

@vpawela
Copy link
Copy Markdown
Author

vpawela commented Apr 9, 2026

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.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

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(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mypy shoould not suggest Foo[...] if annotation contains Foo(arg=...) etc.

1 participant