Skip to content

Commit e81e09b

Browse files
bpo-42233: Correctly repr GenericAlias when used with typing module (GH-23081)
Noticed by @serhiy-storchaka in the bpo. `typing`'s types were not showing the parameterized generic. Eg. previously: ```python >>> typing.Union[dict[str, float], list[int]] 'typing.Union[dict, list]' ``` Now: ```python >>> typing.Union[dict[str, float], list[int]] 'typing.Union[dict[str, float], list[int]]' ``` Automerge-Triggered-By: GH:gvanrossum (cherry picked from commit 1f7dfb2) Co-authored-by: kj <[email protected]>
1 parent 4c239a3 commit e81e09b

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

Lib/test/test_typing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ def test_repr(self):
300300
self.assertEqual(repr(u), repr(int))
301301
u = Union[List[int], int]
302302
self.assertEqual(repr(u), 'typing.Union[typing.List[int], int]')
303+
u = Union[list[int], dict[str, float]]
304+
self.assertEqual(repr(u), 'typing.Union[list[int], dict[str, float]]')
303305

304306
def test_cannot_subclass(self):
305307
with self.assertRaises(TypeError):
@@ -411,6 +413,7 @@ def test_repr(self):
411413
self.assertEqual(repr(Tuple[()]), 'typing.Tuple[()]')
412414
self.assertEqual(repr(Tuple[int, float]), 'typing.Tuple[int, float]')
413415
self.assertEqual(repr(Tuple[int, ...]), 'typing.Tuple[int, ...]')
416+
self.assertEqual(repr(Tuple[list[int]]), 'typing.Tuple[list[int]]')
414417

415418
def test_errors(self):
416419
with self.assertRaises(TypeError):
@@ -483,6 +486,8 @@ def test_repr(self):
483486
self.assertEqual(repr(ct2), 'typing.Callable[[str, float], int]')
484487
ctv = Callable[..., str]
485488
self.assertEqual(repr(ctv), 'typing.Callable[..., str]')
489+
ct3 = Callable[[str, float], list[int]]
490+
self.assertEqual(repr(ct3), 'typing.Callable[[str, float], list[int]]')
486491

487492
def test_callable_with_ellipsis(self):
488493

@@ -2273,6 +2278,8 @@ def test_repr(self):
22732278
self.assertEqual(repr(cv), 'typing.Final[int]')
22742279
cv = Final[Employee]
22752280
self.assertEqual(repr(cv), 'typing.Final[%s.Employee]' % __name__)
2281+
cv = Final[tuple[int]]
2282+
self.assertEqual(repr(cv), 'typing.Final[tuple[int]]')
22762283

22772284
def test_cannot_subclass(self):
22782285
with self.assertRaises(TypeError):

Lib/typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def _type_repr(obj):
160160
typically enough to uniquely identify a type. For everything
161161
else, we fall back on repr(obj).
162162
"""
163+
if isinstance(obj, types.GenericAlias):
164+
return repr(obj)
163165
if isinstance(obj, type):
164166
if obj.__module__ == 'builtins':
165167
return obj.__qualname__
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :func:`repr` of :mod:`typing` types containing
2+
:ref:`Generic Alias Types <types-genericalias>` previously did not show the
3+
parameterized types in the ``GenericAlias``. They have now been changed to do so.

0 commit comments

Comments
 (0)