I'm writing a generic repository class (with pydantic and sqlalchemy) and I'd like to remove the need to supply the result pydantic model as an argument like so:
class DatabaseRepository(Generic[T]):
@classmethod
async def get(cls, obj_id, model_class: Type[T]) -> T:
table = cls.get_table()
async with AsyncSession(cls.engine) as session:
result = await session.get(table, obj_id)
return model_class.from_orm(result)
I have found online that get_args is supposed to allow me to access the model given to the generic class, but it doesn't work for me:
get_args(cls.__bases__)[0].from_orm(result)
cls.__bases__ is an empty list, and the pydantic model isn't there to access. Am I accessing the wrong property? I have tried other properties like __orig_bases__ and that's also an empty list.
Note: T is a Pydantic model derived from BaseModel.
Is there a way to remove the model_class argument I showed above and still use the from_orm() method inside a generic class?