From 9e45b73b6a9675c0670d9cd4f8accc08ab5aa83f Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 4 Apr 2018 23:25:58 +0100 Subject: [PATCH 1/2] Remove the faulty name hack for generic aliases --- Lib/test/test_typing.py | 5 +++-- Lib/typing.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index b12e5ea2fb80f1..390fe60fcc1026 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 3ac3b93822622b..9ae3c8cf9aad74 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,10 @@ 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:] + if orig_name in _normalize_alias: + name = _normalize_alias[orig_name] + else: + name = orig_name self._name = name if not isinstance(params, tuple): params = (params,) From 6bd2860fa2d384280c71059763030f22e9f6655f Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 4 Apr 2018 23:42:11 +0100 Subject: [PATCH 2/2] Use get to simplify code --- Lib/typing.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 9ae3c8cf9aad74..d45502ffee48be 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -642,10 +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__ - if orig_name in _normalize_alias: - name = _normalize_alias[orig_name] - else: - name = orig_name + name = _normalize_alias.get(orig_name, orig_name) self._name = name if not isinstance(params, tuple): params = (params,)