This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kj
Recipients gvanrossum, joperez, kj, serhiy.storchaka
Date 2021-10-10.05:59:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1633845568.2.0.105055482972.issue45418@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-10-10 05:59:28kjsetrecipients: + kj, gvanrossum, serhiy.storchaka, joperez
2021-10-10 05:59:28kjsetmessageid: <1633845568.2.0.105055482972.issue45418@roundup.psfhosted.org>
2021-10-10 05:59:28kjlinkissue45418 messages
2021-10-10 05:59:28kjcreate