FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44524: Do not set _name of _SpecialForm without need by serhiy-storchaka · Pull Request #27861 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 0 additions & 1 deletion Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Fidget-Spinner Aug 21, 2021 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL