I'm using Django 4.1.7 with django-stubs 1.16.0, and mypy 1.1.1.
I have code that looks like this:
class ProductQuerySet(QuerySet):
...
class ProductManager(Manager):
def create_red_product(self, **kwargs) -> "Product":
return self.model(color=Product.Color.RED, **kwargs)
_product_manager = ProductManager.from_queryset(ProductQuerySet)
class Product(Model):
...
objects = _product_manager()
When mypy looks at this, it says:
models/product.py:46: error: Incompatible return value type (got "_T", expected "Product") [return-value]
It seems like the type of self.model
in a model manager method is _T
, which from what I understand is a generic type bound to the model, which in my case should be "Product".
Why isn't this working? How can I fix it?
I can't reproduce one to one your case, but please try to specify what exactly your manager is for by:
class ProductManager(Manager["Product"]):