- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 516
Description
Bug report
What's wrong
Hi! Long time fan, first time caller 👋
I'm having issues with the package resolving related managers when defining custom Managers and Querysets.
I've got a set up of a base class that all the models in the project inherit from. The base class defines the objects manager as the CustomQuerySet.as_manager - CustomQuerySet being a set of common utils for querying models. So far so good. However, I was trying to extend this and add some custom specific Manager behaviour and this is where things started getting a bit more complicated. Because of a very close relationship of Managers and QuerySets I decided to solve it the same way Django does it here
from django.db import models
class CustomManager(models.Manager): ...
class CustomQuerySet(models.QuerySet):
    @classmethod
    def as_manager(cls) -> CustomManager:
        manager = CustomManager.from_queryset(cls)()
        manager._built_with_as_manager = True
        return manager
class BaseModel(models.Model):
     objects = CustomQuerySet.as_manager()From here there are multiple models inheriting from BaseModel, which relate to each other and so on, and it all works fine in Django, but this is where the issues started occuring with type checks, namely this issue occurs
myapp/models:18: error: Couldn't resolve related manager 'my_models' for relation 'myapp.models.MyModel.related'.  [django-manager-missing]
Combined with the fact that I can't just ignore those errors because of this issue has become quite problematic.
I wrote up two test cases illustrating the problem, which you can find here, specifically this test case
Just to quote from the description of the test cases
First case presents no issues. We're adding a custom QuerySet and plugging it into two related model in a base class using the default objects manager and the as_manager() method interface of QuerySets. It's all resolved fine and there are no errors - why this is important will be clearer with the 2nd case.
The second test case is a bit more complex in that we're tweaking the resulting manager in the as_manager. The way it's plugged into the model however doesn't change from the 1st case. Moreover, the manager itself isn't doing much, and it inherits from the default Django manager as well. The problem is that we're now getting two errors from the [django-manager-missing] check which is quite surprising.
The problem is ocurring for me in a medium sized codebase with hundreds of models managed through a shared base class and manager classes. I wouldn't say the intention is overly complex, neither is the implementation, and the fact is that Django's working perfectly fine with it and has no issues setting up the models and managers.
How is that should be
I'd expect the related managers to return correct typing information. Django resolves those managers without an issue, so there must be something in the way mypy_django_plugin builds up the managers that doesn't work. Debugging locally only got me as far for now as here so not very far, but it seems that the default managers for those models are not populated in those cases.
I'll keep trying, but perhaps in the meantime someone with more experience of the codebase might know better.
System information
- OS:
- pythonversion: 3.10, 3.11
- djangoversion: 4.2, 5
- mypyversion: 1.15.0
- django-stubsversion: master + 5.1.0
- django-stubs-extversion: