diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2022-05-04 14:06:50 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-04 14:06:50 (GMT) |
| commit | 9d20e1af409c22537f096206edd330f16ab8f1f3 (patch) | |
| tree | 6b7d928cf568547932a19130cfd309b187e29d93 | |
| parent | 000a072318f8709dfe5c2f8c63dfa4a57f075674 (diff) | |
| download | cpython-9d20e1af409c22537f096206edd330f16ab8f1f3.zip cpython-9d20e1af409c22537f096206edd330f16ab8f1f3.tar.gz cpython-9d20e1af409c22537f096206edd330f16ab8f1f3.tar.bz2 | |
gh-92107: Add tests that subscription works on arbitrary named tuple types (GH-92304)
| -rw-r--r-- | Lib/test/test_collections.py | 12 | ||||
| -rw-r--r-- | Lib/test/test_typing.py | 14 |
2 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 3a16045..fa1d0e0 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -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 diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 55e18c0..8399465 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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) |
