[issue44653] Parameter substitution in the union type does not work with typing.Union

Ken Jin report at bugs.python.org
Mon Jul 19 10:56:50 EDT 2021


Ken Jin <kenjin4096 at gmail.com> added the comment:

@Serhiy, this doesn't just affect typing.Union, it seems that the rest of the typing types don't substitute:

>>> (int | T)[typing.List[str]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Each union arg must be a type, got typing.List[str]

We should probably loosen the check during the union_getitem substitution -- no need to raise the TypeError or check for is_union, just blindly replace the TypeVar. We already do this for types.GenericAlias:

>>> list[T][1]
list[1]

Or if you want to continue checking, maybe checking for PyCallable_Check(obj) in substitution is enough - typing internally accepts callable(o) too:

https://github.com/python/cpython/blob/3.10/Lib/typing.py#L146

----------

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


More information about the Python-bugs-list mailing list