Skip to content
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

Trying to iterate over typing.Union results in hang #92261

Closed
mrahtz opened this issue May 3, 2022 · 2 comments
Closed

Trying to iterate over typing.Union results in hang #92261

mrahtz opened this issue May 3, 2022 · 2 comments
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@mrahtz
Copy link
Contributor

mrahtz commented May 3, 2022

If you accidentally iterate over a Union, you get a hang:

from typing import Union
list(Union)
# ...hang

It looks like it's an infinite loop:

from typing import Union
for i in Union:
    print(i)
# 0, 1, 2, ...

As far as I can tell, this happens because typing.Union is implemented using typing._SpecialForm, and _SpecialForm is implemented using __getitem__ to support parameterisation like Union[int]. Unfortunately, because _SpecialForm doesn't implement __iter__, __getitem__ is also used for iteration - meaning that when trying to iterate, we effectively do Union[0], Union[1], etc.

@mrahtz mrahtz added the type-bug An unexpected behavior, bug, or error label May 3, 2022
@JelleZijlstra
Copy link
Member

We should deprecate itertools.count because it's now redundant.

More seriously, agree that we should disallow this.

@mrahtz
Copy link
Contributor Author

mrahtz commented May 8, 2022

Fixed in 4739997.

@mrahtz mrahtz closed this as completed May 8, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 9, 2022
…pythonGH-92262)

(cherry picked from commit 4739997)

Co-authored-by: Matthew Rahtz <matthew.rahtz@gmail.com>
serhiy-storchaka pushed a commit that referenced this issue May 10, 2022
…2262) (GH-92582)

(cherry picked from commit 4739997)

Co-authored-by: Matthew Rahtz <matthew.rahtz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants