How to typehint custom and overridden Managers in an Abstract Class using django-stubs?

we are trying to add typehints to our django (3.2) project. We are using django-stubs (1.12.0).

We have a BaseModel with a custom manager that we use for almost every other model.

class BaseManager(models.Manager):
    # ...

class AbstractBaseModel(models.Model):
    # ...
    objects = BaseManager()
    # ...

class MyManager(BaseManager):
    # ...

class MyModel(AbstractBaseModel):
    # ...
    objects = MyManager()
    # ...

When I run this through mypy (0.982), I get this error on the objects assignment of MyModel:

Incompatible types in assignment (expression has type "MyManager[MyModel]", 
base class "AbstractBaseModel" defined the type as 

How would I add typehints to this? Thank you!

As I said in my comment above, I could not reproduce that same error you described. It seems that in the django-stubs the BaseManager is defined as a covariant generic type over any Model (sub-)class.

So I would annotate the code you provided as follows:

from __future__ import annotations

from typing import TypeVar

from django.db import models

M = TypeVar("M", bound=models.Model, covariant=True)

class BaseManager(models.Manager[M]):

class AbstractBaseModel(models.Model):
    objects: BaseManager[AbstractBaseModel] = BaseManager()

class MyManager(BaseManager[M]):

class MyModel(AbstractBaseModel):
    objects: BaseManager[MyModel] = MyManager()

This passes mypy --strict without issues.

Versions used:

Back to Top