I'm creating the base repository class and getting the following error:
from typing import Any, Generic, Type, TypeVar
from sqlmodel import Session, SQLModel, select
from app.models.base import BaseModel
from app.models.notification import Notification
from app.types import SQLModelType
T = TypeVar("T", bound=BaseModel)
class BaseCRUD(Generic[T]):
def __init__(self, db: Session, model: Type[BaseModel]) -> None:
self.db = db
self.model = model
def create(self, item: T) -> T:
self.db.add(item)
self.db.commit()
self.db.refresh(item)
return item
def get(self, uuid: Any) -> T:
statement = select(self.model).where(self.model.uuid == uuid)
return self.db.exec(statement).one() <---- Error: Mypy: Incompatible return value type (got "BaseModel", expected "T")
How do I annotate so that mypy knows that it is an object of a subclass of BaseModel? The TypeVar and bound are made for that right? Is it a bug then?