Subclass of Generic fails with AttributeError: object has no attribute '__parameters__' - using Generic when superclass does forward __init_subclass__

2 weeks ago 15
ARTICLE AD BOX

I have a setup like the following

from typing import Generic, TypeVar T = TypeVar("T") class ThirdParty: def __init_subclass__(cls): ... # does not call super() class Mine(ThirdParty, Generic[T]): ... class Sub(Mine[int]): ...

However it fails with the following

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/dsperber/master/repos/SYMPOL/rllib_port/core/stated_flax_model.py", line 27, in <module> class Sub(Mine[ModelType]): File "/home/xxxx/miniconda3/envs/python3.10/lib/python3.10/typing.py", line 309, in inner return cached(*args, **kwds) File "/home/xxxx/miniconda3/envs/python3.10/lib/python3.10/typing.py", line 1342, in __class_getitem__ if any(isinstance(t, ParamSpec) for t in cls.__parameters__): AttributeError: type object 'Mine' has no attribute '__parameters__'

I figured out that this is because ThirdParty has a __init_subclass__ function but it does not call super().__init_subclass__, hence Generic.__init_subclass__ is not called. Is there a way around this? Adding __init_subclass__ to Mine does not work or the following fails as well:

def __init_subclass__(cls): Generic.__init_subclass__(cls) # TypeError: Generic.__init_subclass__() takes 1 positional argument but 2 were given
Read Entire Article