-
-
Notifications
You must be signed in to change notification settings - Fork 458
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
reverse relations are not recognized by mypy #2272
Comments
I've tried to investigate this further, but I'm having trouble trying to figure out if your provided snippets are a repro case or not. Could you please fill in the missing pieces so that we can check if we have a repro case here? From your images:
I'm not saying that you necessarily need to include all model fields, a runnable minimal breaking case is fine. If possible, of course. |
Minimal repro case: from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100, blank=True)
def featured_books(self) -> list[Book]:
return list(self.book_set.filter(featured=True))
class Book(models.Model):
name = models.CharField(max_length=100, blank=True)
featured = models.BooleanField(default=False)
authors = models.ManyToManyField(Author) (and here's a complete Django project: https://github.com/cuu508/django-stubs-2272)
Interestingly, I get this error with django-stubs 5.0.3. With 5.0.2:
|
I can reproduce the problem this way too, but that means that the model isn't registered by Django. And if it isn't registered by Django, I'm afraid that #2272 (comment) isn't a repro case. I can reproduce from #2272 (comment) though. Let me check it out |
Bisected the problem in #2272 (comment) to #2275. There's a fix for that in #2283. But I think all of that is unrelated to the original issue reported here. |
Thanks for the quick fix @flaeppe! Testing django-stubs 5.0.4 with my project, I hit another issue which perhaps requires a similar fix: from __future__ import annotations
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100, blank=True)
def featured_names(self) -> list[str]:
return list(self.book_set.values_list("name", flat=True))
class Book(models.Model):
name = models.CharField(max_length=100, blank=True)
featured = models.BooleanField(default=False)
authors = models.ManyToManyField(Author)
I pushed the repro case to the same project but different branch here: https://github.com/cuu508/django-stubs-2272/tree/values_list |
I added a fix for #2272 (comment) in #2288 |
Awesome, just tested this change, and fixes the issue I saw in my project. |
Hi. I have access to the code that @AlexandrVasilchuk mentioned. I looked into it. Here are the conclusions: You are right. This problem is hard to reproduce. The
the proposals app is among the applications. So, it's loaded. I managed to solve this issue when I added this
After I added this code, mypy stopped giving me an error. Do you have any thoughts about this behavior? As far as I understand, the Proposal model isn't imported when type-checking happens. But how is that possible? |
Is the |
No. I added it for test. Mypy stopped giving me error. Its not mandatory to do this with every model, right? Didnt see this recomendation for django. Also, every other model are not imported to |
AFAIK Django requires every model to be registered to be discoverable via a I'm going to close now since that fixed the problem |
Bug report
Description
I am facing an issue with django-stubs where reverse relations are not recognized by mypy. Despite trying multiple threads and solutions, including those in the following discussions and issues:
Discussion 2192
Issue 150
The problem persists.
Actual Behavior
mypy raises errors indicating that the reverse relation field is not recognized. Specifically, it cannot resolve the keyword 'proposals' into the field.
Expected Behavior
mypy should recognize the reverse relations between models and not raise errors when these relations are used.
Steps to Reproduce
Define models with ManyToManyField and reverse relations.
Run mypy to check for typing errors.
System information
python
3.11.4:django
4.2.7,:mypy
"1.10.1":django-stubs
"5.0.2":django-stubs-ext
"5.0.2":How can I make mypy recognize the reverse relation between these models? It is also important to mention that the models implement standard objects.
Thank you for your assistance.
The text was updated successfully, but these errors were encountered: