-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GenericAlias does not support nested type variables #84588
Comments
While trying to replace typing._GenericAlias with GenericAlias I have found that the latter does not support nested type variables. >>> from typing import *
>>> T = TypeVar('T')
>>> X = List[List[T]]
>>> X.__parameters__
(~T,)
>>> X[int]
typing.List[typing.List[int]]
>>> Y = list[list[T]]
>>> Y.__parameters__
()
>>> Y[int]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: There are no type variables left in list[list[~T]] |
Good catch. Is there a reasonable fix? |
Not yet. If you don't prefer to fix it yourself, I'll do it as soon as I have time. |
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. |
FWIW, to be most close to the static type checkers behavior, both D[int][str] and D[int, str] should fail for D = Dict[T, List]. Not important however, since this is a really rare corner case I think. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: