-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
GenericAlias does not support union type expressions #86399
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
Comments
Union type expressions added in PEP-604 throw an error when both operands are GenericAlias objects. Eg the following works:: int | list[str] The following throws TypeError:: list[int] | dict[float, str]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'types.GenericAlias' I have submitted a PR to fix this. Coincidentally, it also fixes the fact that union expressions weren't de-duplicating GenericAlias properly. Eg::
>>> list[int] | int | list[int]
list[int] | int | list[int] For non-GenericAlias type expressions, the new code shouldn't be much slower. Rich compare is only used for GenericAlias objects. This isn't very scientific, but python -m timeit "int | str | float" # original # purely rich compare # check for GenericAlias and rich compare only for that |
There is also a problem with typing module. >>> typing.List[int] | dict[float, str]
typing.Union[typing.List[int], dict] |
@serhiy, wow interesting find, it seems to be typing's repr problem rather than the actual types itself: >>> typing.Union[dict[int, str], list[str]]
typing.Union[dict, list]
>>> typing.Union[dict[int, str], list[str]].__args__
(dict[int, str], list[str]) The __args__ seem to be correct, so I'm guessing the typing repr went wrong somewhere. That should be the case for your example too: >>> alias = typing.List[int] | dict[float, str]
>>> alias
typing.Union[typing.List[int], dict]
>>> type(alias)
<class 'typing._UnionGenericAlias'>
>>> alias.__args__
(typing.List[int], dict[float, str]) I'll work on this. If I don't reply back in a week, someone else is free to take over this issue. |
Guys, any chance the fix will land in 3.9? It is affected as well. |
@kj Sorry, for some reason, I thought the that issue affected Union as well, but I have just re-tested it and found that it works. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: