1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Subclass of Generic fails with AttributeError: object has no attribute...

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página