[Python] Using classmethods, class objects and generics together with mypy

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I use the following code to create subclasses of class G and pass them the type of model they should produce.

    from typing import TypeVar, Type


    class A:
    @classmethod
    def model_validate(cls):
    print('Done')


    T = TypeVar('T', bound=A)


    class G[T]:
    def __init__(self, model: Type[T]):
    self.model = model

    def func(self) -> None:
    print(self.model.model_validate())


    G[A](A).func()


    This works fine, but mypy gives this error:

    error: "type[T]" has no attribute "model_validate" [attr-defined]


    What am i doing wrong?

    Continue reading...

Compartilhe esta Página