Skip to content

[3.10] bpo-44524: Do not set _name of _SpecialForm without need (GH-27861) #27871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4873,7 +4873,6 @@ def test_special_attrs(self):
self.assertEqual(cls.__name__, name, str(cls))
self.assertEqual(cls.__qualname__, name, str(cls))
self.assertEqual(cls.__module__, 'typing', str(cls))
self.assertEqual(getattr(cls, '_name', name), name, str(cls))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(cls, proto)
loaded = pickle.loads(s)
Expand Down
19 changes: 8 additions & 11 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class Starship:
be used with isinstance() or issubclass().
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _GenericAlias(self, (item,), name="ClassVar")
return _GenericAlias(self, (item,))

@_SpecialForm
def Final(self, parameters):
Expand All @@ -474,7 +474,7 @@ class FastConnector(Connection):
There is no runtime checking of these properties.
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _GenericAlias(self, (item,), name="Final")
return _GenericAlias(self, (item,))

@_SpecialForm
def Union(self, parameters):
Expand Down Expand Up @@ -512,12 +512,9 @@ def Union(self, parameters):
parameters = _remove_dups_flatten(parameters)
if len(parameters) == 1:
return parameters[0]

if len(parameters) == 2 and type(None) in parameters:
name = "Optional"
else:
name = "Union"
return _UnionGenericAlias(self, parameters, name=name)
return _UnionGenericAlias(self, parameters, name="Optional")
return _UnionGenericAlias(self, parameters)

@_SpecialForm
def Optional(self, parameters):
Expand Down Expand Up @@ -562,7 +559,7 @@ def open_helper(file: str, mode: MODE) -> str:
except TypeError: # unhashable parameters
pass

return _LiteralGenericAlias(self, parameters, name="Literal")
return _LiteralGenericAlias(self, parameters)


@_SpecialForm
Expand Down Expand Up @@ -601,7 +598,7 @@ def Concatenate(self, parameters):
"ParamSpec variable.")
msg = "Concatenate[arg, ...]: each arg must be a type."
parameters = tuple(_type_check(p, msg) for p in parameters)
return _ConcatenateGenericAlias(self, parameters, name="Concatenate")
return _ConcatenateGenericAlias(self, parameters)


@_SpecialForm
Expand Down Expand Up @@ -649,7 +646,7 @@ def is_str(val: Union[str, float]):
PEP 647 (User-Defined Type Guards).
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _GenericAlias(self, (item,), name="TypeGuard")
return _GenericAlias(self, (item,))


class ForwardRef(_Final, _root=True):
Expand Down Expand Up @@ -960,7 +957,7 @@ def __mro_entries__(self, bases):

def __getattr__(self, attr):
if attr in {'__name__', '__qualname__'}:
return self._name
return self._name or self.__origin__.__name__

# We are careful for copy and pickle.
# Also for simplicity we just don't relay all dunder names
Expand Down