-
- Notifications
You must be signed in to change notification settings - Fork 33.6k
Closed
Labels
Description
If you accidentally iterate over a Union, you get a hang:
from typing import Union list(Union) # ...hangIt 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.