Skip to content

Commit

Permalink
Ensure ManyToManyField related managers supports renamed imports (#2153)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaeppe committed May 13, 2024
1 parent be51acb commit 6ff16d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mypy_django_plugin/transformers/manytomany.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import NamedTuple, Optional, Tuple, Union

from mypy.checker import TypeChecker
from mypy.nodes import AssignmentStmt, Expression, MemberExpr, NameExpr, StrExpr, TypeInfo
from mypy.nodes import AssignmentStmt, Expression, MemberExpr, NameExpr, RefExpr, StrExpr, TypeInfo
from mypy.plugin import FunctionContext, MethodContext
from mypy.semanal import SemanticAnalyzer
from mypy.types import Instance, ProperType, TypeVarType, UninhabitedType
Expand Down Expand Up @@ -129,7 +129,7 @@ def get_model_from_expression(
Attempts to resolve an expression to a 'TypeInfo' instance. Any lazy reference
argument(e.g. "<app_label>.<object_name>") to a Django model is also attempted.
"""
if isinstance(expr, NameExpr) and isinstance(expr.node, TypeInfo):
if isinstance(expr, RefExpr) and isinstance(expr.node, TypeInfo):
if helpers.is_model_type(expr.node):
return Instance(expr.node, [])

Expand Down
33 changes: 33 additions & 0 deletions tests/typecheck/fields/test_related.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1340,3 +1340,36 @@
class MyModel(ConcreteParent, AbstractParent):
m2m_5 = models.ManyToManyField(Other, related_name="mymodel")
- case: test_m2m_related_managers_supports_renamed_imports
main: |
from myapp.models import MyModel
from other.models import Other
reveal_type(MyModel.m2m_1.through.objects) # N: Revealed type is "django.db.models.manager.Manager[myapp.models.MyModel_m2m_1]"
reveal_type(Other.auto_through.through.objects) # N: Revealed type is "django.db.models.manager.Manager[myapp.models.MyModel_m2m_1]"
reveal_type(MyModel.m2m_2.through.objects) # N: Revealed type is "django.db.models.manager.Manager[myapp.models.Through]"
reveal_type(Other.custom_through.through.objects) # N: Revealed type is "django.db.models.manager.Manager[myapp.models.Through]"
installed_apps:
- other
- myapp
files:
- path: other/__init__.py
- path: other/models.py
content: |
from django.db import models
class Other(models.Model):
...
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
from other import models as other_models
class Through(models.Model):
mymodel = models.ForeignKey("myapp.MyModel", on_delete=models.CASCADE)
other = models.ForeignKey(other_models.Other, on_delete=models.CASCADE)
class MyModel(models.Model):
m2m_1 = models.ManyToManyField(other_models.Other, related_name="auto_through")
m2m_2 = models.ManyToManyField(other_models.Other, related_name="custom_through", through=Through)

0 comments on commit 6ff16d9

Please sign in to comment.