diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 8ddb17e2..5dc32517 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1551,6 +1551,14 @@ def _non_annotations(nodes: list[ast.AST]) -> None: else: _non_annotation(kwd) + elif _is_typing(node.func, 'assert_type', self.scopeStack): + _non_annotation(node.func) + + # assert_type(val, "tp") + _non_annotations(node.args[:1]) + _annotations(node.args[1:]) + _non_annotations(node.keywords) + elif _is_typing(node.func, 'TypeVar', self.scopeStack): _non_annotation(node.func) _non_annotations(node.args[:1]) @@ -1565,6 +1573,34 @@ def _non_annotations(nodes: list[ast.AST]) -> None: else: _non_annotation(kwd) + elif ( + _is_typing(node.func, 'ParamSpec', self.scopeStack) or + _is_typing(node.func, 'TypeVarTuple', self.scopeStack) + ): + _non_annotation(node.func) + _non_annotations(node.args) + + # ParamSpec("P", default=..., bound=...) + # TypeVarTuple("Ts", default=..., bound=...) + for kwd in node.keywords: + if kwd.arg in ('bound', 'default'): + _annotation(kwd) + else: + _non_annotation(kwd) + + elif _is_typing(node.func, 'NewType', self.scopeStack): + _non_annotation(node.func) + _non_annotations(node.args[:1]) + + # NewType("NT", "C") + _annotations(node.args[1:]) + + for kwd in node.keywords: + if kwd.arg == 'tp': + _annotation(kwd) + else: + _non_annotation(kwd) + elif _is_typing(node.func, "TypedDict", self.scopeStack): _non_annotation(node.func) _non_annotations(node.args[:1]) diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py index 26010ca2..b86bdca5 100644 --- a/pyflakes/test/test_type_annotations.py +++ b/pyflakes/test/test_type_annotations.py @@ -555,6 +555,35 @@ def test_typevar_undefined_arg0(self): T = TypeVar(T, int) """, m.UndefinedName) + def test_newtype_quoted_types(self): + self.flakes(""" + from typing import NewType + from u import C, D + NT1 = NewType("NT1", "C") + NT2 = NewType("NT2", tp="D") + """) + + def test_typevartuple_quoted_types(self): + self.flakes(""" + from typing import TypeVarTuple + from u import C, D + Ts = TypeVarTuple("Ts", default="C", bound="D") + """) + + def test_paramspec_quoted_types(self): + self.flakes(""" + from typing import ParamSpec + from u import C, D + P = ParamSpec("P", default="C", bound="D") + """) + + def test_assert_type_quoted_types(self): + self.flakes(""" + from typing import assert_type + from u import C + assert_type(1, "C") + """) + def test_literal_type_typing(self): self.flakes(""" from typing import Literal