[Python-checkins] bpo-32873: Remove a name hack for generic aliases in typing module (GH-6376)

Miss Islington (bot) webhook-mailer at python.org
Wed Apr 4 20:46:43 EDT 2018


https://github.com/python/cpython/commit/04eac02088f60192c7e54c7364bcaa892d7c05cf
commit: 04eac02088f60192c7e54c7364bcaa892d7c05cf
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-04-04T17:46:40-07:00
summary:

bpo-32873: Remove a name hack for generic aliases in typing module (GH-6376)


This removes a hack and replaces it with a proper
mapping {'list': 'List', 'dict': 'Dict', ...}.
(cherry picked from commit 2a363d2930e29ec6d8a774973ed5a4965f881f5f)

Co-authored-by: Ivan Levkivskyi <levkivskyi at gmail.com>

files:
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 b12e5ea2fb80..390fe60fcc10 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1058,14 +1058,15 @@ class C(B[int]):
             self.assertEqual(x.bar, 'abc')
             self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
         samples = [Any, Union, Tuple, Callable, ClassVar,
-                   Union[int, str], ClassVar[List], Tuple[int, ...], Callable[[str], bytes]]
+                   Union[int, str], ClassVar[List], Tuple[int, ...], Callable[[str], bytes],
+                   typing.DefaultDict, typing.FrozenSet[int]]
         for s in samples:
             for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                 z = pickle.dumps(s, proto)
                 x = pickle.loads(z)
                 self.assertEqual(s, x)
         more_samples = [List, typing.Iterable, typing.Type, List[int],
-                        typing.Type[typing.Mapping]]
+                        typing.Type[typing.Mapping], typing.AbstractSet[Tuple[int, str]]]
         for s in more_samples:
             for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                 z = pickle.dumps(s, proto)
diff --git a/Lib/typing.py b/Lib/typing.py
index 3ac3b9382262..d45502ffee48 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -611,6 +611,19 @@ def __reduce__(self):
 # * __args__ is a tuple of all arguments used in subscripting,
 #   e.g., Dict[T, int].__args__ == (T, int).
 
+
+# Mapping from non-generic type names that have a generic alias in typing
+# but with a different name.
+_normalize_alias = {'list': 'List',
+                    'tuple': 'Tuple',
+                    'dict': 'Dict',
+                    'set': 'Set',
+                    'frozenset': 'FrozenSet',
+                    'deque': 'Deque',
+                    'defaultdict': 'DefaultDict',
+                    'type': 'Type',
+                    'Set': 'AbstractSet'}
+
 def _is_dunder(attr):
     return attr.startswith('__') and attr.endswith('__')
 
@@ -629,7 +642,7 @@ def __init__(self, origin, params, *, inst=True, special=False, name=None):
         self._special = special
         if special and name is None:
             orig_name = origin.__name__
-            name = orig_name[0].title() + orig_name[1:]
+            name = _normalize_alias.get(orig_name, orig_name)
         self._name = name
         if not isinstance(params, tuple):
             params = (params,)



More information about the Python-checkins mailing list