Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1f7dfb2

Browse files
authoredNov 1, 2020
bpo-42233: Correctly repr GenericAlias when used with typing module (pythonGH-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
1 parent 148c76b commit 1f7dfb2

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed
 

‎Lib/test/test_typing.py

+7
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):
@@ -407,6 +409,7 @@ def test_repr(self):
407409
self.assertEqual(repr(Tuple[()]), 'typing.Tuple[()]')
408410
self.assertEqual(repr(Tuple[int, float]), 'typing.Tuple[int, float]')
409411
self.assertEqual(repr(Tuple[int, ...]), 'typing.Tuple[int, ...]')
412+
self.assertEqual(repr(Tuple[list[int]]), 'typing.Tuple[list[int]]')
410413

411414
def test_errors(self):
412415
with self.assertRaises(TypeError):
@@ -479,6 +482,8 @@ def test_repr(self):
479482
self.assertEqual(repr(ct2), 'typing.Callable[[str, float], int]')
480483
ctv = Callable[..., str]
481484
self.assertEqual(repr(ctv), 'typing.Callable[..., str]')
485+
ct3 = Callable[[str, float], list[int]]
486+
self.assertEqual(repr(ct3), 'typing.Callable[[str, float], list[int]]')
482487

483488
def test_callable_with_ellipsis(self):
484489

@@ -2269,6 +2274,8 @@ def test_repr(self):
22692274
self.assertEqual(repr(cv), 'typing.Final[int]')
22702275
cv = Final[Employee]
22712276
self.assertEqual(repr(cv), 'typing.Final[%s.Employee]' % __name__)
2277+
cv = Final[tuple[int]]
2278+
self.assertEqual(repr(cv), 'typing.Final[tuple[int]]')
22722279

22732280
def test_cannot_subclass(self):
22742281
with self.assertRaises(TypeError):

‎Lib/typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def _type_repr(obj):
162162
typically enough to uniquely identify a type. For everything
163163
else, we fall back on repr(obj).
164164
"""
165+
if isinstance(obj, types.GenericAlias):
166+
return repr(obj)
165167
if isinstance(obj, type):
166168
if obj.__module__ == 'builtins':
167169
return obj.__qualname__
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)
Please sign in to comment.