Skip to content

bpo-44524: Do not set _name of _SpecialForm without need #27861

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
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 @@ -4929,7 +4929,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 @@ -461,7 +461,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 @@ -482,7 +482,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 @@ -520,12 +520,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)
Comment on lines +524 to +525
Copy link
Member

@Fidget-Spinner Fidget-Spinner Aug 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will there be a subtle difference between how the two behaves since one sets name and the other doesn't?

I would think not but I'm not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting _name affects subclassing and pickling/copying. Union[] and Optional[] are not subclassable, and special implementation of __reduce__ was added in previous commits. If there are other difference, I do not know.


@_SpecialForm
def Optional(self, parameters):
Expand Down Expand Up @@ -570,7 +567,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 @@ -609,7 +606,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 @@ -657,7 +654,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 @@ -962,7 +959,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__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is so nice. I wish I'd thought about this a little deeper when I proposed the original one:(.

Anyways if I'm not wrong, __origin__ will always be set on all GenericAlias to some type, so this should always succeed.


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