Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@mrahtz

Description

@mrahtz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions