diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index e896df518447c5..d7c558a289ef2d 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -8468,6 +8468,15 @@ class ThisWontWorkEither(NamedTuple): 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 1a2ef8c086f772..ebcc9b7968a13f 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3044,8 +3044,7 @@ class Employee(NamedTuple): """ 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 00000000000000..45561898e2e1e9 --- /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.