Message403573
I don't understand your example, T | None doesn't return a types.Union object, it returns typing.Union/typing.Optional. (I'm assuming this T is the TypeVar in typing). Which *is* subscriptable.
>>> (T | None)[int].__origin__
typing.Union
If you meant to say: why is typing.Union[] allowed, but not types.UnionType[]? That is intentional. types.UnionType is only meant for builtin types. Once you union with *any* type from typing, it will convert to a typing.Union.
>>> type(int | str)
<class 'types.UnionType'>
>>> int | str | T
typing.Union[int, str, ~T]
If you intend to reconstruct a types.Union from another types.Union, you can do:
args = get_args(int | str)
import operator, functools
functools.reduce(operator.or_, args)
And guard this code with an isinstance(tp, types.UnionType) check. |
|
Date |
User |
Action |
Args |
2021-10-10 05:59:28 | kj | set | recipients:
+ kj, gvanrossum, serhiy.storchaka, joperez |
2021-10-10 05:59:28 | kj | set | messageid: <1633845568.2.0.105055482972.issue45418@roundup.psfhosted.org> |
2021-10-10 05:59:28 | kj | link | issue45418 messages |
2021-10-10 05:59:28 | kj | create | |
|