-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
ForwardRef does not support | operator #89652
Comments
Not positive this is a bug, but it seems like ForwardRef should support the pipe (|) operator for indicating Unions in Python 3.10: Python 3.10.0 (default, Oct 4 2021, 22:09:55) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import ForwardRef
>>> ForwardRef('asdf') | ForwardRef('fdsa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'ForwardRef' and 'ForwardRef'
>>> int | str
int | str
>>> |
--- a/Lib/typing.py + def __or__(self, other): This can be easily fixed, but I am waiting for guido and ken to check whether this is an intentioned operation. |
I suppose this is useful for programs that use runtime annotations (e.g. pydantic?). I don’t mind adding this. |
ForwardRef is for internal use, and writing ForwardRef('fdsa') is discouraged, as the docs say: This class should not be instantiated by a user. However, I don't see any cons from adding union support, so let's do it! Note that a workaround exists for OP's example. But it may not be sufficient for complex runtime libraries: ForwardRef('asdf | fdsa') |
Do we need to create a backport for 3.10? |
No. :-) |
Thanks @corona10 for the patch. @bobbeyreese |
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: