From a868e1c263e06434599e488ab5b2d5249467b1f3 Mon Sep 17 00:00:00 2001 From: Dexter Kennedy Date: Tue, 23 Apr 2024 19:26:06 -0400 Subject: [PATCH] misleading error message --- mypy/fastparse.py | 4 ++++ test-data/unit/check-errorcodes.test | 27 +++++++++++++++++++++++++++ test-data/unit/check-fastparse.test | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index e208e4d0b7d9..a3f655d59af1 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1830,6 +1830,10 @@ def visit_Call(self, e: Call) -> Type: f = e.func constructor = stringify_name(f) + if self.parent() is None: + return self.invalid_type( + e, note="Suggestion: Don't use function calls in type annotations" + ) if not isinstance(self.parent(), ast3.List): note = None if constructor: diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 9d49480539e0..1c642602b79f 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -51,6 +51,33 @@ 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] +from typing import Dict +from typing_extensions import Annotated + +class _EnvironmentVariables(): + def __init__(self, variables: Dict) -> None: + self.__variables = variables + +def EnvironmentVariables(sort: bool): + return Annotated[_EnvironmentVariables, dict] + +def unsorted_env_variables(variables: EnvironmentVariables(sort=False)) -> None: # E: Invalid type comment or annotation [valid-type] \ + # N: Suggestion: Don't use function calls in type annotations + return variables.as_json_obj() + +[builtins fixtures/tuple.pyi] + +class _EnvironmentVariables(): + def __init__(self, variables: dict[str, bytes]) -> None: + self.__variables = variables + +def EnvironmentVariables(sort: bool): + return Annotated[_EnvironmentVariables, dict] + +def unsorted_env_variables(variables: EnvironmentVariables(sort=False)) -> None: + return variables.as_json_obj() + [case testErrorCodeSyntaxErrorIgnoreNote] # This is a bit inconsistent -- syntax error would be more logical? x: 'a b' # type: ignore[valid-type] diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index 534967b1edbf..fb9872345665 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -205,7 +205,7 @@ def f(a: Foo(int)) -> int: pass [out] main:7: error: Invalid type comment or annotation -main:7: note: Suggestion: use Foo[...] instead of Foo(...) +main:7: note: Suggestion: Don't use function calls in type annotations [case testFastParseMatMul]