diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 7af8dcd526df81..049fade8a72880 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -493,6 +493,7 @@ def __getnewargs__(self): '_asdict': _asdict, '__getnewargs__': __getnewargs__, '__match_args__': field_names, + '__class_getitem__': None, } for index, name in enumerate(field_names): doc = _sys.intern(f'Alias for field number {index}') diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 3a16045c5aa1ae..cb5db01b5c7703 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -520,6 +520,11 @@ def test_tupleness(self): with self.assertRaises(AttributeError): p.z + def test_non_generic(self): + Point = namedtuple('Point', 'x y') + with self.assertRaises(TypeError): + Point[int, str] + def test_odd_sizes(self): Zero = namedtuple('Zero', '') self.assertEqual(Zero(), ()) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index bf96ba065fbb04..d119019ebbdc54 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -116,7 +116,6 @@ class BaseTest(unittest.TestCase): TemporaryDirectory, SpooledTemporaryFile, Queue, SimpleQueue, _AssertRaisesContext, - SplitResult, ParseResult, WeakSet, ReferenceType, ref, ShareableList, Future, _WorkItem, @@ -138,7 +137,7 @@ def test_subscriptable(self): self.assertEqual(alias.__parameters__, ()) def test_unsubscriptable(self): - for t in int, str, float, Sized, Hashable: + for t in int, str, float, Sized, Hashable, SplitResult, ParseResult: tname = t.__name__ with self.subTest(f"Testing {tname}"): with self.assertRaisesRegex(TypeError, tname): diff --git a/Misc/NEWS.d/next/Library/2022-05-01-17-10-36.gh-issue-92107.RmsD1n.rst b/Misc/NEWS.d/next/Library/2022-05-01-17-10-36.gh-issue-92107.RmsD1n.rst new file mode 100644 index 00000000000000..9175110f20b647 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-01-17-10-36.gh-issue-92107.RmsD1n.rst @@ -0,0 +1,2 @@ +:class:`collections.namedtuple` types are non-generic by default. +Subscripting them now raises TypeError.