[issue40408] GenericAlias does not support nested type variables

Serhiy Storchaka report at bugs.python.org
Sat May 2 14:03:40 EDT 2020


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

There is a difference between PR 19836 and the typing module in handling nested unsubscribed generic aliases:

>>> from typing import *
>>> T = TypeVar('T')
>>> D1 = Dict[T, List]
>>> D2 = dict[T, List]
>>> D1.__parameters__
(~T,)
>>> D1[int]
typing.Dict[int, typing.List[~T]]
>>> D1[int].__parameters__
(~T,)
>>> D1[int][str]
typing.Dict[int, typing.List[str]]
>>> D1[int, str]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/typing.py", line 267, in inner
    return func(*args, **kwds)
  File "/home/serhiy/py/cpython/Lib/typing.py", line 686, in __getitem__
    _check_generic(self, params)
  File "/home/serhiy/py/cpython/Lib/typing.py", line 221, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
TypeError: Too many parameters for typing.Dict[~T, typing.List]; actual 2, expected 1
>>> D2.__parameters__
(~T, ~T)
>>> D2[int]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Too few arguments for dict[~T, typing.List]
>>> D2[int, str]
dict[int, typing.List[str]]

But this behavior is not specified and is not covered by tests.

----------

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


More information about the Python-bugs-list mailing list