mirror of
https://github.com/python/cpython.git
synced 2025-10-01 12:52:18 +00:00
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 2a363d2930
)
Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
This commit is contained in:
parent
bd85c97382
commit
04eac02088
2 changed files with 17 additions and 3 deletions
|
@ -1058,14 +1058,15 @@ class GenericTests(BaseTestCase):
|
||||||
self.assertEqual(x.bar, 'abc')
|
self.assertEqual(x.bar, 'abc')
|
||||||
self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
|
self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
|
||||||
samples = [Any, Union, Tuple, Callable, ClassVar,
|
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 s in samples:
|
||||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
z = pickle.dumps(s, proto)
|
z = pickle.dumps(s, proto)
|
||||||
x = pickle.loads(z)
|
x = pickle.loads(z)
|
||||||
self.assertEqual(s, x)
|
self.assertEqual(s, x)
|
||||||
more_samples = [List, typing.Iterable, typing.Type, List[int],
|
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 s in more_samples:
|
||||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
z = pickle.dumps(s, proto)
|
z = pickle.dumps(s, proto)
|
||||||
|
|
|
@ -611,6 +611,19 @@ class TypeVar(_Final, _Immutable, _root=True):
|
||||||
# * __args__ is a tuple of all arguments used in subscripting,
|
# * __args__ is a tuple of all arguments used in subscripting,
|
||||||
# e.g., Dict[T, int].__args__ == (T, int).
|
# 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):
|
def _is_dunder(attr):
|
||||||
return attr.startswith('__') and attr.endswith('__')
|
return attr.startswith('__') and attr.endswith('__')
|
||||||
|
|
||||||
|
@ -629,7 +642,7 @@ class _GenericAlias(_Final, _root=True):
|
||||||
self._special = special
|
self._special = special
|
||||||
if special and name is None:
|
if special and name is None:
|
||||||
orig_name = origin.__name__
|
orig_name = origin.__name__
|
||||||
name = orig_name[0].title() + orig_name[1:]
|
name = _normalize_alias.get(orig_name, orig_name)
|
||||||
self._name = name
|
self._name = name
|
||||||
if not isinstance(params, tuple):
|
if not isinstance(params, tuple):
|
||||||
params = (params,)
|
params = (params,)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue