Skip to content

Commit

Permalink
misleading error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexter Kennedy committed Apr 23, 2024
1 parent 400eece commit a868e1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit a868e1c

Please sign in to comment.