gh-92261: Disallow iteration of Union (and other _SpecialForms)#92262
gh-92261: Disallow iteration of Union (and other _SpecialForms)#92262serhiy-storchaka merged 10 commits intopython:mainfrom
Conversation
serhiy-storchaka
left a comment
There was a problem hiding this comment.
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? |
gvanrossum
left a comment
There was a problem hiding this comment.
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 <matthew.rahtz@gmail.com>
|
GH-92582 is a backport of this pull request to the 3.11 branch. |
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__ = Noneon a number of other things intyping.py, to prevent similar errors with other things.