]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-144321: Fix named tuple bug when input is a non-sequence iterable (#144600)
authorbkap123 <97006829+bkap123@users.noreply.github.com>
Wed, 11 Feb 2026 12:44:22 +0000 (07:44 -0500)
committerGitHub <noreply@github.com>
Wed, 11 Feb 2026 12:44:22 +0000 (12:44 +0000)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst [new file with mode: 0644]

index 72ae7776ab90628771b4fad405d3011a1f38b277..50938eadc8f9f38a64dd12bd152e3836142be69c 100644 (file)
@@ -8532,6 +8532,15 @@ class NamedTupleTests(BaseTestCase):
                 def name(self):
                     return __class__.__name__
 
+    def test_named_tuple_non_sequence_input(self):
+        field_names = ["x", "y"]
+        field_values = [int, int]
+        Point = NamedTuple("Point", zip(field_names, field_values))
+        p = Point(1, 2)
+        self.assertEqual(p.x, 1)
+        self.assertEqual(p.y, 2)
+        self.assertEqual(repr(p),"Point(x=1, y=2)")
+
 
 class TypedDictTests(BaseTestCase):
     def test_basics_functional_syntax(self):
index 71a08a5f1df8114cfc58c5014d2dd498327beee6..2dfa6d3b1499ca11771fc458feac2d0f64be3736 100644 (file)
@@ -3075,8 +3075,7 @@ def NamedTuple(typename, fields, /):
     """
     types = {n: _type_check(t, f"field {n} annotation must be a type")
              for n, t in fields}
-    field_names = [n for n, _ in fields]
-    nt = _make_nmtuple(typename, field_names, _make_eager_annotate(types), module=_caller())
+    nt = _make_nmtuple(typename, types, _make_eager_annotate(types), module=_caller())
     nt.__orig_bases__ = (NamedTuple,)
     return nt
 
diff --git a/Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst b/Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst
new file mode 100644 (file)
index 0000000..4556189
--- /dev/null
@@ -0,0 +1,3 @@
+The functional syntax for creating :class:`typing.NamedTuple`
+classes now supports passing any :term:`iterable` of fields and types.
+Previously, only sequences were supported.