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

Include ModelBase subclasses in plugin base class hook condition #1863

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,9 @@ def resolve_lazy_reference(


def is_model_type(info: TypeInfo) -> bool:
return info.metaclass_type is not None and info.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME
return info.metaclass_type is not None and (
# Using the model metaclass shipped by Django
info.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME
# Using a custom model metaclass that inherits above
or info.metaclass_type.type.has_base(fullnames.MODEL_METACLASS_FULLNAME)
flaeppe marked this conversation as resolved.
Show resolved Hide resolved
)
22 changes: 22 additions & 0 deletions tests/typecheck/models/test_metaclass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,25 @@

class Concrete3(Concrete2):
...

- case: test_custom_model_base_metaclass
main: |
from myapp.models import This, Other

this = This(field=Other())
reveal_type(this.field) # N: Revealed type is "myapp.models.Other"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
from django.db.models.base import ModelBase

class MyBase(ModelBase): ...
class MyModel(models.Model, metaclass=MyBase): ...

class Other(MyModel): ...
class This(MyModel):
field = models.ForeignKey(Other, on_delete=models.CASCADE)