Django, MyPy: Incompatible types in assignment (UserManager)

134 views Asked by At

I have following errors when I am running mypy checking:

error: Cannot override class variable (previously declared on base class "User") with instance variable  [misc]
error: Incompatible types in assignment (expression has type "type[BuyerManager[Any]]", base class "User" defined the type as "UserManager[User]")  [assignment]

My code is following:

class User(AbstractUser):
    ...


class Buyer(User):
    objects = BuyerManager()


class BuyerManager(UserManager):
    def get_queryset(self) -> QuerySet:
        return super().get_queryset().filter(status="buyer")

Can't find solution. So, any help or advice welcomed)

The result of my research: here found that the mypy issues error for this case because of it's not match to the Liskov substitution principle, so I did like recomended there: objects = BuyerManager() # type: ignore

Or maybe I should create a buyers filter method instead of overriding get_queryset and use it if necessary.

Anyway, if you have better solution please share, thanks!

0

There are 0 answers