[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Serhiy Storchaka
Serhiy Storchaka 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

[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Guido van Rossum
Guido van Rossum added the comment: Oh, I see. No, they must make another special case, like for Annotated.-- --Guido (mobile) -- ___ Python tracker ___

[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Ken Jin
Ken Jin added the comment: @Guido, I hope I didn't misunderstand you, but to clarify, what OP is asking is an alternative way to construct types.UnionType objects and write: types.UnionType[int, str] like how we used to write before 3.10: typing.Union[int, str] I don't know why we need

[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Guido van Rossum
Guido van Rossum added the comment: > 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. But why? Just so

[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Ken Jin
Ken Jin added the comment: No worries! > So I'll have to add some `if` in my code. Yeah, we had to do that in the typing module too. Hope you manage to fix your library without much trouble. -- ___ Python tracker

[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Joseph Perez
Joseph Perez added the comment: Indeed, sorry, my example was bad. My library was raising at several place, and I've extrapolated about generic substitution. I've indeed other substitutions (without `TypeVar`), and because they were failing, I've assumed that all of my substitutions were

[issue45418] types.UnionType is not subscriptable

2021-10-10 Thread Ken Jin
Ken Jin 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

[issue45418] types.UnionType is not subscriptable

2021-10-09 Thread Joseph Perez
New submission from Joseph Perez : `types.UnionType` is not subscriptable, and this is an issue when type manipulations are done. A common maniputation I've to do is to substitute all the `TypeVar` of a potential generic type by their specialization in the given context. For example, given a