Content-Length: 278443 | pFad | https://github.com/python/cpython/issues/92261

ad Trying to iterate over typing.Union results in hang · Issue #92261 · python/cpython · GitHub
Skip to content

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

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









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/92261

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy