From 8f0796aa0226ea61d5c38739bed9b263d2762f3d Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Wed, 7 Oct 2026 14:47:20 -0400 Subject: [PATCH] handle runtime types for NamedTuple / TypedDict arg 1 --- pyflakes/checker.py | 8 ++++++-- pyflakes/test/test_type_annotations.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 76a245b2..f8eef751 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1585,7 +1585,9 @@ def _non_annotations(nodes: list[ast.AST]) -> None: for k, v in iter_dict_children(node.args[1]): _non_annotation(k) _annotation(v) - _non_annotations(node.args[2:]) + _non_annotations(node.args[2:]) + else: + _non_annotations(node.args[1:]) # TypedDict("a", a=int) if sys.version_info >= (3, 13): # pragma: >=3.13 cover @@ -1608,7 +1610,9 @@ def _non_annotations(nodes: list[ast.AST]) -> None: _annotations(elt.elts[1:]) else: _non_annotation(elt) - _non_annotations(node.args[2:]) + _non_annotations(node.args[2:]) + else: + _non_annotations(node.args[1:]) # NamedTuple("a", a=int) if sys.version_info >= (3, 15): # pragma: >=3.15 cover diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py index 29b8ba07..093144fd 100644 --- a/pyflakes/test/test_type_annotations.py +++ b/pyflakes/test/test_type_annotations.py @@ -797,6 +797,13 @@ def test_typeddict_key_value_ordering(self): TypedDict("TD", {"k": (y := "V"), y: "V"}) """) + def test_typeddict_not_dict(self): + self.flakes(""" + from typing import TypedDict + from x import y + TypedDict("TD", y) + """) + def test_namedtypes_classes(self): self.flakes(""" from typing import TypedDict, NamedTuple @@ -815,6 +822,14 @@ def test_namedtuple_non_tuple_args(self): NT = NamedTuple("NT", [x]) """) + def test_namedtuple_non_list_args(self): + self.flakes(""" + from typing import NamedTuple + from x import y + + NamedTuple("NT", y) + """) + @skipIf(version_info < (3, 11), 'new in Python 3.11') def test_variadic_generics(self): # pragma: >=3.11 cover self.flakes("""