Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix misleading error message: MyPy should not suggest a syntactic error when function calls are included in type annotations #17160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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