-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
gh-92261: Disallow iteration of Union (and other _SpecialForms) #92262
gh-92261: Disallow iteration of Union (and other _SpecialForms) #92262
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about list(list)
?
[Serhiy]
I've added a couple of tests to |
For me, the style is too verbose, but the code does everything right, and I don't want to be too picky. |
Should we apply this to 3.11 too? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a pure bugfix it seems rather involved. And since it is only intending to report a better error for something that's clearly invalid, I am not inclined to backport it. (Even though it has so many pieces that it will likely cause a fair amount of merge issues for future, simpler, bugfixes that do deserve being backported.
It's not just an error message though: on 3.11 |
Okay, then I'll leave it to your judgment (and that of @pablogsal). Be sure to also backport the one-line fix for slots. :-) |
Thanks @mrahtz for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11. |
…pythonGH-92262) (cherry picked from commit 4739997) Co-authored-by: Matthew Rahtz <[email protected]>
GH-92582 is a backport of this pull request to the 3.11 branch. |
…2262) (GH-92582) (cherry picked from commit 4739997) Co-authored-by: Matthew Rahtz <[email protected]>
As #92261 describes,
list(Union)
currently results in a hang because_SpecialForm.__getitem__
is used for iteration, meaning thatlist(Union)
effectively doesUnion[0], Union[1], ...
.This PR fixes it by implementing
_SpecialForm.__iter__
, which takes priority over_SpecialForm.__getitem__
when iterating.Edit: and now also explicitly setting
__iter__ = None
on a number of other things intyping.py
, to prevent similar errors with other things.