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

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

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:typing-specialform-name
Aug 21, 2021
Merged

serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:typing-specialform-name

Conversation

serhiy-storchaka commented Aug 20, 2021 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

Because setting non-empty _name affects other behavior.

In most cases __name__ can be derived from __origin__.__name__.

https://bugs.python.org/issue44524

Becase setting non-empty _name affects other behavior.

In most cases __name__ can be derived from __origin__.__name__.

Copy link
Copy Markdown
Member

What does Ken Jin say?

Fidget-Spinner left a comment

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

LGTM. Personally I don't think setting _name on _SpecialForms will break anything (because they aren't subclassable, and any methods they need are overridden, so their MRO doesn't matter). But I've been wrong on multiple occasions at this point 😉 , so let's just be safe. From a technical standpoint, Serhiy's solution is much cleaner (and faster) too.

PS @serhiy-storchaka , you can do the same for Annotated and copy the test over from my PR #27841. I'm closing that so we don't have to deal with merging multiple PRs.

Comment thread Lib/typing.py
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.

Comment thread Lib/typing.py
Comment on lines +524 to +525
return _UnionGenericAlias(self, parameters, name="Optional")
return _UnionGenericAlias(self, parameters)

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.

serhiy-storchaka merged commit 4ceec49 into python:main Aug 21, 2021

Copy link
Copy Markdown
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

serhiy-storchaka deleted the typing-specialform-name branch August 21, 2021 06:48

Copy link
Copy Markdown

GH-27871 is a backport of this pull request to the 3.10 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 21, 2021
)

Because setting non-empty _name affects behavior of other code.

In most cases __name__ can be derived from __origin__.__name__.
(cherry picked from commit 4ceec49)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

Copy link
Copy Markdown
Member Author

you can do the same for Annotated and copy the test over from my PR #27841.

It is easier to merge them separately. There are no conflicts.

serhiy-storchaka added a commit that referenced this pull request Aug 21, 2021
…H-27871)

Because setting non-empty _name affects behavior of other code.

In most cases __name__ can be derived from __origin__.__name__.
(cherry picked from commit 4ceec49)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
farcat mannequin mentioned this pull request Apr 14, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL