File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
mypy_django_plugin/transformers Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,36 @@ def use_my_model() -> int:
191191 return foo.xyz # Gives an error
192192```
193193
194+ ### Why am I getting incompatible return type errors on my custom managers?
195+
196+ If you declare your custom managers without generics and override built-in
197+ methods you might see an error message about incompatible error messages,
198+ something like this:
199+
200+ ``` python
201+ from django.db import models
202+
203+ class MyManager (model .Manager ):
204+ def create (self , ** kwargs ) -> " MyModel" :
205+ pass
206+ ```
207+
208+ will cause this error message:
209+
210+ ```
211+ error: Return type "MyModel" of "create" incompatible with return type "_T" in supertype "BaseManager"
212+ ```
213+
214+ This is happening because the ` Manager ` class is generic, but without
215+ specifying generics the built-in manager methods are expected to return the
216+ generic type of the base manager, which is any model. To fix this issue you
217+ should declare your manager with your model as the type variable:
218+
219+ ``` python
220+ class MyManager (models .Manager[" MyModel" ]):
221+ ...
222+ ```
223+
194224### How do I annotate cases where I called QuerySet.annotate?
195225
196226Django-stubs provides a special type, ` django_stubs_ext.WithAnnotations[Model] ` , which indicates that the ` Model ` has
Original file line number Diff line number Diff line change @@ -480,6 +480,9 @@ class MyManager(models.Manager): ...
480480
481481 _T = TypeVar('_T', covariant=True)
482482 class MyManager(models.Manager[_T]): ...
483+
484+ Note that this does not happen if mypy is run with disallow_any_generics = True,
485+ as not specifying the generic type is then considered an error.
483486 """
484487
485488 manager = ctx .api .lookup_fully_qualified_or_none (ctx .cls .fullname )
You can’t perform that action at this time.
0 commit comments