Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Closed
Description
If you accidentally iterate over aUnion, you get a hang:
fromtypingimportUnionlist(Union)# ...hang
It looks like it's an infinite loop:
fromtypingimportUnionforiinUnion:print(i)# 0, 1, 2, ...
As far as I can tell, this happens becausetyping.Union is implemented usingtyping._SpecialForm, and_SpecialForm is implemented using__getitem__ to support parameterisation likeUnion[int]. Unfortunately, because_SpecialForm doesn't implement__iter__,__getitem__ is also used for iteration - meaning that when trying to iterate, we effectively doUnion[0],Union[1], etc.