I’m having issue with type-hinting the Generic class I’ve implemented.

ModelType = TypeVar("ModelType", bound=Base)  # pylint: disable=invalid-name
CreateSchemaType = TypeVar(  # pylint: disable=invalid-name
    "CreateSchemaType", bound=BaseModel
)
UpdateSchemaType = TypeVar(  # pylint: disable=invalid-name
    "UpdateSchemaType", bound=BaseModel
)

class BaseOperations(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
    """Base operations class for the database."""

    __abstract__ = True

    def __init__(self, model: Type[ModelType], db_session: Session):
        """Initialize the operations class."""
        self.model = model
        self.db_session = db_session

    def get_by_id(self, id_: int) -> ModelType | None:
        """Get a single instance by ID."""
        if hasattr(self, "deleted"):
            return (
                self.db_session.query(self.model)
                .filter(~self.deleted)
                .filter(self.model.id == id_)
                .first()
            )
        return self.db_session.query(self.model).filter(self.model.id == id_).first()

    def get_by_ids(self, ids: list[int]) -> List[ModelType]:
        """Get a list of instances by IDs."""
        if hasattr(self, "deleted"):
            return (
                self.db_session.query(self.model)
                .filter(~self.deleted)
                .filter(self.model.id.in_(ids))
                .all()
            )
        return self.db_session.query(self.model).filter(self.model.id.in_(ids)).all()

Mypy throws the error:

error: “Type[ModelType]” has no attribute “id” [attr-defined]

If I hover over self.model, it shows me the type as:

(variable) model: type[ModelType@BaseOperations]

I’m a bit new with typehinting in Python, so would appreciate any help here. Thanks.