Skip to content

Commit

Permalink
gh-92107: Add tests that subscription works on arbitrary named tuple …
Browse files Browse the repository at this point in the history
…types (GH-92304)
  • Loading branch information
serhiy-storchaka committed May 4, 2022
1 parent 000a072 commit 9d20e1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Lib/test/test_collections.py
Expand Up @@ -698,6 +698,18 @@ def test_match_args(self):
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
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_typing.py
Expand Up @@ -5736,6 +5736,20 @@ class Y(Generic[T], NamedTuple):
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)
Expand Down

0 comments on commit 9d20e1a

Please sign in to comment.