[Python-checkins] bpo-42233: Correctly repr GenericAlias when used with typing module (GH-23081)

miss-islington webhook-mailer at python.org
Sat Nov 7 11:46:17 EST 2020


https://github.com/python/cpython/commit/e81e09bfc8a29a44a649a962870a26fe0c097cfa
commit: e81e09bfc8a29a44a649a962870a26fe0c097cfa
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-11-07T08:46:08-08:00
summary:

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 1f7dfb277e5b88cddc13e5024766be787a3e9127)

Co-authored-by: kj <28750310+Fidget-Spinner at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2020-11-02-01-31-15.bpo-42233.YxRj-h.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 7f96aff710455..67cadc37e4fbe 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -300,6 +300,8 @@ def test_repr(self):
         self.assertEqual(repr(u), repr(int))
         u = Union[List[int], int]
         self.assertEqual(repr(u), 'typing.Union[typing.List[int], int]')
+        u = Union[list[int], dict[str, float]]
+        self.assertEqual(repr(u), 'typing.Union[list[int], dict[str, float]]')
 
     def test_cannot_subclass(self):
         with self.assertRaises(TypeError):
@@ -411,6 +413,7 @@ def test_repr(self):
         self.assertEqual(repr(Tuple[()]), 'typing.Tuple[()]')
         self.assertEqual(repr(Tuple[int, float]), 'typing.Tuple[int, float]')
         self.assertEqual(repr(Tuple[int, ...]), 'typing.Tuple[int, ...]')
+        self.assertEqual(repr(Tuple[list[int]]), 'typing.Tuple[list[int]]')
 
     def test_errors(self):
         with self.assertRaises(TypeError):
@@ -483,6 +486,8 @@ def test_repr(self):
         self.assertEqual(repr(ct2), 'typing.Callable[[str, float], int]')
         ctv = Callable[..., str]
         self.assertEqual(repr(ctv), 'typing.Callable[..., str]')
+        ct3 = Callable[[str, float], list[int]]
+        self.assertEqual(repr(ct3), 'typing.Callable[[str, float], list[int]]')
 
     def test_callable_with_ellipsis(self):
 
@@ -2273,6 +2278,8 @@ def test_repr(self):
         self.assertEqual(repr(cv), 'typing.Final[int]')
         cv = Final[Employee]
         self.assertEqual(repr(cv), 'typing.Final[%s.Employee]' % __name__)
+        cv = Final[tuple[int]]
+        self.assertEqual(repr(cv), 'typing.Final[tuple[int]]')
 
     def test_cannot_subclass(self):
         with self.assertRaises(TypeError):
diff --git a/Lib/typing.py b/Lib/typing.py
index 39c956dd1834d..6fd67b038834e 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -160,6 +160,8 @@ def _type_repr(obj):
     typically enough to uniquely identify a type.  For everything
     else, we fall back on repr(obj).
     """
+    if isinstance(obj, types.GenericAlias):
+        return repr(obj)
     if isinstance(obj, type):
         if obj.__module__ == 'builtins':
             return obj.__qualname__
diff --git a/Misc/NEWS.d/next/Library/2020-11-02-01-31-15.bpo-42233.YxRj-h.rst b/Misc/NEWS.d/next/Library/2020-11-02-01-31-15.bpo-42233.YxRj-h.rst
new file mode 100644
index 0000000000000..aad4249fa165b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-11-02-01-31-15.bpo-42233.YxRj-h.rst
@@ -0,0 +1,3 @@
+The :func:`repr` of :mod:`typing` types containing 
+:ref:`Generic Alias Types <types-genericalias>` previously did not show the 
+parameterized types in the ``GenericAlias``.  They have now been changed to do so.



More information about the Python-checkins mailing list