Reconcile sqlalchemy Mapped[type] vs type with mypy

76 Views Asked by At

Im trying to separate responsabilities on my Flask app. For this im trying to implement the logic of the models on mixins away from its definitios, so the models ulimately inherit the majority of their behavior and methods from the mixins.

Examples of this separation are given in this remarkable blog from Bob Waycott

Given the following context:

app/domain/user/mixins/user.py

class UserMixin:
    name: str
    last: str

    # .... A lot of other important methdos for this type of object
    def fullname(self) -> str:
        return f"{self.name} {self.last}"

Till here mypy is passing all good.

app/models/user.py

from app.domain.user.mixins.user import UserMixin

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    
    name: Mapped[str]
    last: Mapped[str]

Here when i check on mypy throws error Incompatible types in assignment (expression has type "Mapped[str]", base class "UserMixin" defined the type as "str")

The question finally is: Is there any way of reconcile Mixins types with Models Mapped[types]?

0

There are 0 best solutions below