]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92107: Add tests that subscription works on arbitrary named tuple types (GH-92304)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 4 May 2022 14:06:50 +0000 (17:06 +0300)
committerGitHub <noreply@github.com>
Wed, 4 May 2022 14:06:50 +0000 (17:06 +0300)
Lib/test/test_collections.py
Lib/test/test_typing.py

index 3a16045c5aa1ae548bae827fc20d9f169e179c89..fa1d0e014dee923f0f3539a31375b95db9225f1f 100644 (file)
@@ -698,6 +698,18 @@ class TestNamedTuple(unittest.TestCase):
         Point = namedtuple('Point', 'x y')
         self.assertEqual(Point.__match_args__, ('x', 'y'))
 
+    def test_non_generic_subscript(self):
+        # For backward compatibility, subscription works
+        # on arbitrary named tuple types.
+        Group = collections.namedtuple('Group', 'key group')
+        A = Group[int, list[int]]
+        self.assertEqual(A.__origin__, Group)
+        self.assertEqual(A.__parameters__, ())
+        self.assertEqual(A.__args__, (int, list[int]))
+        a = A(1, [2])
+        self.assertIs(type(a), Group)
+        self.assertEqual(a, (1, [2]))
+
 
 ################################################################################
 ### Abstract Base Classes
index 55e18c08537df72c356eafa757ff278b7a890f7a..8399465f6052da82ad641afaf42653fb153983d0 100644 (file)
@@ -5736,6 +5736,20 @@ class NamedTupleTests(BaseTestCase):
                 with self.assertRaises(TypeError):
                     G[int, str]
 
+    def test_non_generic_subscript(self):
+        # For backward compatibility, subscription works
+        # on arbitrary NamedTuple types.
+        class Group(NamedTuple):
+            key: T
+            group: list[T]
+        A = Group[int]
+        self.assertEqual(A.__origin__, Group)
+        self.assertEqual(A.__parameters__, ())
+        self.assertEqual(A.__args__, (int,))
+        a = A(1, [2])
+        self.assertIs(type(a), Group)
+        self.assertEqual(a, (1, [2]))
+
     def test_namedtuple_keyword_usage(self):
         LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
         nick = LocalEmployee('Nick', 25)