Forward References in UnionType?
With Union, forward references in strings get converted to ForwardRef. To wit:
from typing import Union Alias = Union[str, "Foo"] Alias typing.Union[str, ForwardRef('Foo')]
With the new UnionType, the following is not valid:
Alias = str | "Foo" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for |: 'type' and 'str'
The following is also invalid:
from typing import ForwardRef Alias = str | ForwardRef("Foo") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for |: 'type' and 'ForwardRef'
Having the whole type alias as a string doesn't quite make sense to me, but maybe it's in fact correct:
Alias = "str | Foo" Alias 'str | Foo'
I see this open BPO, which was discussed in 2021: https://bugs.python.org/issue45857 Any suggestions on how else to make forward references in type aliases with the new UnionType?
participants (1)
-
Paul Bryan