From 6ec2c3b6d7258f36af9572a2ffc5de41b0270a1b Mon Sep 17 00:00:00 2001 From: Brij Date: Sun, 8 Feb 2026 12:08:08 -0500 Subject: [PATCH 1/7] fix named tuple bug --- Lib/test/test_typing.py | 9 +++++++++ Lib/typing.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index e896df518447c5..fdf5588cb88df5 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_generator_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..1905f0a9fa66cc 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3044,7 +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] + field_names = list(types) nt = _make_nmtuple(typename, field_names, _make_eager_annotate(types), module=_caller()) nt.__orig_bases__ = (NamedTuple,) return nt From 5916e008083685c9dbf870b5a4618f592cce390d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 8 Feb 2026 17:09:12 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst 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..fd9e709c9d49df --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst @@ -0,0 +1 @@ +``NamedTuple`` function now works if the ``fields`` argument is a generator From 791fa9def3c2c50503b66fcbc650216d2e9809dc Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Sun, 8 Feb 2026 14:21:32 -0500 Subject: [PATCH 3/7] Update Misc/NEWS.d/next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst Co-authored-by: sobolevn --- .../Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index fd9e709c9d49df..4e6c10ee3ef1d2 100644 --- 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 @@ -1 +1,2 @@ -``NamedTuple`` function now works if the ``fields`` argument is a generator +Fix :class:`typing.NamedTuple`` incorrectly +inferring its fields when passing a generator. From f8ab1d275ab81bbce989d376d7cf3a91b61e6da7 Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Sun, 8 Feb 2026 14:25:21 -0500 Subject: [PATCH 4/7] Update 2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst --- .../next/Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 4e6c10ee3ef1d2..1181904ae8987b 100644 --- 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 @@ -1,2 +1,2 @@ -Fix :class:`typing.NamedTuple`` incorrectly +Fix :class:`typing.NamedTuple` incorrectly inferring its fields when passing a generator. From 02c3c48972b9a76a64226ac28da8383ef8a302f0 Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Sun, 8 Feb 2026 16:08:47 -0500 Subject: [PATCH 5/7] Update 2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst --- .../Library/2026-02-08-17-09-10.gh-issue-144321.w58PhQ.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 index 1181904ae8987b..45561898e2e1e9 100644 --- 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 @@ -1,2 +1,3 @@ -Fix :class:`typing.NamedTuple` incorrectly -inferring its fields when passing a generator. +The functional syntax for creating :class:`typing.NamedTuple` +classes now supports passing any :term:`iterable` of fields and types. +Previously, only sequences were supported. From ea59497bae8979b33bf26079405d95ce18049113 Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Sun, 8 Feb 2026 16:15:59 -0500 Subject: [PATCH 6/7] Update Lib/test/test_typing.py Co-authored-by: Alex Waygood --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index fdf5588cb88df5..d7c558a289ef2d 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -8468,7 +8468,7 @@ class ThisWontWorkEither(NamedTuple): def name(self): return __class__.__name__ - def test_named_tuple_generator_input(self): + def test_named_tuple_non_sequence_input(self): field_names = ["x", "y"] field_values = [int, int] Point = NamedTuple("Point", zip(field_names, field_values)) From 722a7ba30d3e69d83772dbd0e66aa6192f568a20 Mon Sep 17 00:00:00 2001 From: bkap123 <97006829+bkap123@users.noreply.github.com> Date: Sun, 8 Feb 2026 16:53:58 -0500 Subject: [PATCH 7/7] Apply suggestion from @AlexWaygood Co-authored-by: Alex Waygood --- Lib/typing.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 1905f0a9fa66cc..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 = list(types) - 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