Ken Jin <kenjin4...@gmail.com> added the comment:

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.

----------
nosy: +gvanrossum, kj, serhiy.storchaka
status: open -> pending

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45418>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to