Skip to content

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

@serhiy-storchaka serhiy-storchaka commented Aug 20, 2021 •

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__.
@gvanrossum

Copy link
Copy Markdown
Member

What does Ken Jin say?

@Fidget-Spinner 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.

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.

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 Fidget-Spinner Aug 21, 2021 •

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.

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.

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
serhiy-storchaka merged commit 4ceec49 into python:main Aug 21, 2021
@miss-islington

Copy link
Copy Markdown
Contributor

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

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

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>
@serhiy-storchaka

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 farcat mannequin mentioned this pull request Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

6 participants