Skip to content

Commit 04eac02

Browse files
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 2a363d2) Co-authored-by: Ivan Levkivskyi <[email protected]>
1 parent bd85c97 commit 04eac02

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Lib/test/test_typing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,14 +1058,15 @@ class C(B[int]):
10581058
self.assertEqual(x.bar, 'abc')
10591059
self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
10601060
samples = [Any, Union, Tuple, Callable, ClassVar,
1061-
Union[int, str], ClassVar[List], Tuple[int, ...], Callable[[str], bytes]]
1061+
Union[int, str], ClassVar[List], Tuple[int, ...], Callable[[str], bytes],
1062+
typing.DefaultDict, typing.FrozenSet[int]]
10621063
for s in samples:
10631064
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10641065
z = pickle.dumps(s, proto)
10651066
x = pickle.loads(z)
10661067
self.assertEqual(s, x)
10671068
more_samples = [List, typing.Iterable, typing.Type, List[int],
1068-
typing.Type[typing.Mapping]]
1069+
typing.Type[typing.Mapping], typing.AbstractSet[Tuple[int, str]]]
10691070
for s in more_samples:
10701071
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10711072
z = pickle.dumps(s, proto)

Lib/typing.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,19 @@ def __reduce__(self):
611611
# * __args__ is a tuple of all arguments used in subscripting,
612612
# e.g., Dict[T, int].__args__ == (T, int).
613613

614+
615+
# Mapping from non-generic type names that have a generic alias in typing
616+
# but with a different name.
617+
_normalize_alias = {'list': 'List',
618+
'tuple': 'Tuple',
619+
'dict': 'Dict',
620+
'set': 'Set',
621+
'frozenset': 'FrozenSet',
622+
'deque': 'Deque',
623+
'defaultdict': 'DefaultDict',
624+
'type': 'Type',
625+
'Set': 'AbstractSet'}
626+
614627
def _is_dunder(attr):
615628
return attr.startswith('__') and attr.endswith('__')
616629

@@ -629,7 +642,7 @@ def __init__(self, origin, params, *, inst=True, special=False, name=None):
629642
self._special = special
630643
if special and name is None:
631644
orig_name = origin.__name__
632-
name = orig_name[0].title() + orig_name[1:]
645+
name = _normalize_alias.get(orig_name, orig_name)
633646
self._name = name
634647
if not isinstance(params, tuple):
635648
params = (params,)

0 commit comments

Comments
 (0)