we are trying to add typehints to our django (3.2) project. We are using django-stubs (1.12.0).
We have an AbstractBaseModel
with a custom BaseManager
that we use for almost every other model. In some cases we also extend the custom Manager.
Example:
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
"AbstractBaseModel_BaseManager2[AbstractBaseModel]")
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
theBaseManager
is defined as a covariant generic type over anyModel
(sub-)class.So I would annotate the code you provided as follows:
This passes
mypy --strict
without issues.Versions used: