From 53cfee2c0dac1c7eeb84be29d2c9fb1e8b178996 Mon Sep 17 00:00:00 2001 From: Yosof Badr <23705518+YosofBadr@users.noreply.github.com> Date: Thu, 16 Apr 2026 04:28:09 +0900 Subject: [PATCH] fix: don't suggest Foo[...] when Foo(arg=...) is used When a function call with keyword arguments appears in a type annotation (e.g. `Foo(sort=True)`), mypy was suggesting to use `Foo[...]` instead. This suggestion is misleading because `Foo[sort=True]` is a syntax error -- the user likely intended a function call, not a type parameterization. Now, when keyword arguments are present, mypy shows "Cannot use a function call in a type annotation" instead. The `Foo[...]` suggestion is preserved for calls with only positional arguments (e.g. `Foo(int)`) where the user likely meant `Foo[int]`. Fixes #16506 --- mypy/fastparse.py | 5 ++++- test-data/unit/check-fastparse.test | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index e85b8fffaf9e9..d9e2d5df8f4c1 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1958,7 +1958,10 @@ def visit_Call(self, e: Call) -> Type: if not isinstance(self.parent(), ast3.List): note = None if constructor: - note = "Suggestion: use {0}[...] instead of {0}(...)".format(constructor) + if e.keywords: + note = "Cannot use a function call in a type annotation" + else: + note = "Suggestion: use {0}[...] instead of {0}(...)".format(constructor) return self.invalid_type(e, note=note) if not constructor: self.fail(message_registry.ARG_CONSTRUCTOR_NAME_EXPECTED, e.lineno, e.col_offset) diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index 7ee5a9c432169..79318d24c8704 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -207,6 +207,17 @@ def f(a: Foo(int)) -> int: main:7: error: Invalid type comment or annotation main:7: note: Suggestion: use Foo[...] instead of Foo(...) +[case testFasterParseCallWithKeywordArgs_no_native_parse] + +def Foo(sort: bool) -> type: + return int + +def f(a: Foo(sort=True)) -> int: + pass +[out] +main:5: error: Invalid type comment or annotation +main:5: note: Cannot use a function call in a type annotation + [case testFastParseMatMul] from typing import Any