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
typing: Classes that inherit Generic[...]
indirectly aren't considered generic.
#82640
Comments
Given the file from typing import Generic, TypeVar
KT = TypeVar("KT")
VT = TypeVar("VT")
class GenericMapping(Generic[KT, VT]):
pass
class SomeImplMapping(GenericMapping):
pass
a: GenericMapping[int, float]
b: SomeImplMapping[int, float] I would expect Traceback (most recent call last):
File "adt.py", line 18, in <module>
b: SomeImplMapping[int, float]
File "/usr/local/lib/python3.7/typing.py", line 254, in inner
return func(*args, **kwds)
File "/usr/local/lib/python3.7/typing.py", line 841, in __class_getitem__
_check_generic(cls, params)
File "/usr/local/lib/python3.7/typing.py", line 204, in _check_generic
raise TypeError(f"{cls} is not a generic class")
TypeError: <class '__main__.SomeImplMapping'> is not a generic class If I understand everything correctly, that's because https://github.com/python/cpython/blob/master/Lib/typing.py#L210 I did not found the restriction that only direct childs of |
However, if I change the signature to: class SomeImplMapping(GenericMapping[KT, VT]): Everything works just fine. And that's not really clear for me: we already have declared the generic types, we can not change the amount of those arguments, so why do we have to pass the same generic parameters again? And even if that's the designed way to do things, I would expect the first example to fail not because of the type substitution, but for deriving the |
BTW I don't mind creating a PR for that issue if there will be an agreement on what is the desired behavior here. |
The docs for typing module are clear about this:
There is also an example involving a base class. |
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: