From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Wed, 11 Feb 2026 12:44:22 +0000 (-0500) Subject: gh-144321: Fix named tuple bug when input is a non-sequence iterable (#144600) X-Git-Tag: v3.15.0a7~309^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=17ab556e39e9b40c7d4308664de42f0564048044;p=thirdparty%2FPython%2Fcpython.git gh-144321: Fix named tuple bug when input is a non-sequence iterable (#144600) --- diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 72ae7776ab90..50938eadc8f9 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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): diff --git a/Lib/typing.py b/Lib/typing.py index 71a08a5f1df8..2dfa6d3b1499 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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 index 000000000000..45561898e2e1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst @@ -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.