Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,9 @@ def visit_Call(self, e: Call) -> Type:

if not isinstance(self.parent(), ast3.List):
note = None
if constructor:
if constructor and not e.keywords:
# Only suggest Foo[...] when there are no keyword arguments,
# since Foo[arg=val] is a SyntaxError (see #16506).
note = "Suggestion: use {0}[...] instead of {0}(...)".format(constructor)
return self.invalid_type(e, note=note)
if not constructor:
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ for v in x: # type: int, int # E: Syntax error in type annotation [syntax] \
# N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
pass

[case testErrorCodeSyntaxError4_no_native_parse]
# Keyword arguments in type annotation should not suggest Foo[...] (see #16506)
def f(a: Foo(arg=int)) -> int: # E: Invalid type comment or annotation [valid-type]
pass

[case testErrorCodeSyntaxErrorIgnoreNote]
# This is a bit inconsistent -- syntax error would be more logical?
x: 'a b' # type: ignore[valid-type]
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ def f(a: Foo(int)) -> int:
main:7: error: Invalid type comment or annotation
main:7: note: Suggestion: use Foo[...] instead of Foo(...)

[case testFasterParseTypeErrorCustomWithKeyword_no_native_parse]

from typing import TypeVar, Generic
T = TypeVar('T')
class Foo(Generic[T]):
pass

def f(a: Foo(arg=int)) -> int:
pass
[out]
main:7: error: Invalid type comment or annotation

[case testFastParseMatMul]

from typing import Any
Expand Down
Loading