[issue45418] types.UnionType is not subscriptable

Serhiy Storchaka report at bugs.python.org
Sun Oct 10 14:36:04 EDT 2021


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

types.UnionType corresponds to typing._UnionGenericAlias, not typing.Union.

We can make (int | str | T)[dict] returning an instance of types.UnionType instead of an instance of typing._UnionGenericAlias. But it will be a breaking change, because typing._UnionGenericAlias and types.UnionType are different and not completely compatible types. We should wait some time before making such changes, so all user code will be made supporting both typing._UnionGenericAlias and types.UnionType.

If the user code does something special like substituting `int | Collection[int]` to `int | list[int]`, it should have some additional ifs in any case, otherwise it will not recognize new typing types including types.UnionTypes. And subscription does not work in all typing types, we have copy_with() for some types and special cases for others in the code of the typing module. I am going to unify it finally, but it takes time, my time and user's time to migrate to new idioms.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45418>
_______________________________________


More information about the Python-bugs-list mailing list