-
-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
error: Couldn't resolve related manager for relation on python3.11 and django3.2 #1354
Comments
Are you sure you're getting typing on that setup? I introduced this error message a while back, but it was only to surface issues that were previously silently ignored. Basically, this error means that the plugin was not able to statically figure out what class the manager would be. Typically that means you're doing something in your code that's not statically checkable (or not supported by the plugin). Are you able to create a minimal example that reproduces the issue? |
ooh, right. I noticed something else, this will happen if the reverse FK class inherit from this 3rd party class https://github.com/jazzband/django-model-utils/blob/cced1c7aeaf97fa6ee5c7e44c8ea827ed10b2da5/model_utils/models.py#L19 so the simple code will looks like: # apps/foos/models.py
from django.db import models
class Foo(models.Model):
field1 = models.PositiveSmallIntegerField() # apps/bars/models.py
from django.db import models
from model_utils.models import TimeStampedModel
class BarQueryset(models.QuerySet):
def get_active_qs(self) -> models.QuerySet:
return self.filter(is_active=True)
class Bar(TimeStampedModel):
foo = models.ForeignKey("foos.Foo", related_name="bars", on_delete=models.CASCADE)
is_active = models.BooleanField(default=False)
objects = models.Manager.from_queryset(BarQueryset)() this will results in: removing the custom but, it will be fine if I changed the from django.db import models
class BarQueryset(models.QuerySet):
def get_active_qs(self) -> models.QuerySet:
return self.filter(is_active=True)
class BaseBar(models.Model):
name = models.CharField(max_length=255)
class Bar(BaseBar):
foo = models.ForeignKey("foos.Foo", related_name="bars", on_delete=models.CASCADE)
is_active = models.BooleanField(default=False)
objects = models.Manager.from_queryset(BarQueryset)() |
Ah, alright. I guess the error message could be improved here, but basically it appears that |
I didn't look into this in detail, but maybe it helps to add manual type hints to the Model class for these manager attributes? |
I was able to fix this issue by annotating the related name. In this case it would be class Foo:
field1 = models.PositiveSmallIntegerField()
bars: "QuerySet[Bar]"
# or if related_name isn't set:
bar_set: "QuerySet[Bar]" |
Annotating with |
I deem that either any of the suggested approaches with explicit annotation is a resolution or if there's 3rd party code involved(ref: #1354 (comment)) this is a duplicate of #1023. Feel free to correct me if I'm wrong. |
This would throw |
Yeah, annotating with The other suggestion of:
seems to somewhat work, but in practice I've had to do the following, or I get from typing import TYPE_CHECKING
class Foo:
field1 = models.PositiveSmallIntegerField()
if TYPE_CHECKING:
from apps.bars.models import Bar # Avoid circular imports
bars: QuerySet[Bar] However, this does require a bunch of manual effort and redundancy. (For instance, in my app, the User model has a custom Side note: This issue has surfaced for me both for models that have an Is there any possibility this could be re-opened with a goal of having this resolution be automatic and more precise in django-stubs? I've sadly stuck with django-stubs 4.2.0 still due to this change in behavior (and a different ManyToMany |
I'm going to go with my gut feeling here and suggest that;
I would claim that newer version(s) have made this more precise. If you're having troubles with newer version(s) and my suggestions above don't help: please open a new issue describing the problem(s) you're running in to with the newer versions and we'll try to help you out. |
Thanks @flaeppe for replying. I hadn't noticed that caveat from those release notes about the change in import path for that type; good to know. Regardless, it still feels like a lot of manual effort to define every reverse relation's type (especially since it has to be done in a Re the ManyToManyField problem, I had seen the GH issue you mentioned, and unfortunately in my case, it occurs for fields that directly reference a model class (not using strings), and also for ones that don't specify a |
I don't know what type checker you're using. But if you're using mypy and have configured the mypy plugin I'm afraid you'd have to create a new issue for the problem with |
Strange, okay, I use mypy and the django-stubs I tried installing other versions, and this reverse relations problem is introduced as of django-stubs 4.2.4 (tested with mypy 1.5.1). django-stubs 4.2.3 and mypy 1.4.1 do not present this problem. If I'm able, I can spend some time putting together an example that demonstrates both of the problems I've described above (reverse relations leading to "Couldn't resolve related manager", and |
Unfortuantly I'm able to replicate, including introducing the bug when moving between 4.2.3 -> 4.2.4 |
any updates here? |
Not sure why this was closed, but I have run into the same issue, even trying the This is with models subclassing |
@danjac I have exactly the same issue. For some reason, it could be worked around by changing class MyModel(TimeStampedModel):
... to class _TSM(models.Models if TYPE_CHECKING else TimeStampedModel):
class Meta:
abstract = True
class MyModel(_TSM):
... but that doesn't seem reasonable, I would prefer to wait until this is fixed (or reverted, the only thing I wanted from 5.0.4 is mypy 1.11 support). Update to 5.0.4 also breaks some related managers resolution for models without inheritance, I can't clarify the reason as for now |
I found I had the same issue for plain Ended up (in pyright) just setting |
We're also running into this issue as well if you bump to the latest stubs and mypy extension you will be able to reproduce the error. https://github.com/BetterAngelsLA/monorepo/tree/main/apps/betterangels-backend |
Bug report
What's wrong
I got
Couldn't resolve related manager for relation
error when using python3.11 and django3.2.So the model looks like
I will get the following error:
dummy/apps/foos/models.py:###: error: Couldn't resolve related manager for relation 'bars' (from dummy.apps.bars.models.Bar.bars.Bar.foo). [django-manager-missing]
It seems only happen when the reverse foreign key has a custom
objects
likeBar
above...And may be related to #1285
I have to use
# type: ignore
for now to silent this...How is that should be
Should have no problem. I tried to downgrade python back to python3.9, mypy==0.812, and django-stubs==1.8.0 and it has no issue.
System information
python
version: 3.11django
version: 3.2.16mypy
version: 0.991django-stubs
version: 1.14.0django-stubs-ext
version: 0.7.0The text was updated successfully, but these errors were encountered: