Skip to content
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

Ensure ManyToManyField related managers supports renamed imports #2153

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading