Skip to content

Commit

Permalink
pass handles_type_annotations_on_3rd_party_models
Browse files Browse the repository at this point in the history
  • Loading branch information
Niicck committed Nov 10, 2024
1 parent 3e810f3 commit c200c4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ def is_model_type(info: TypeInfo) -> bool:
return info.metaclass_type is not None and info.metaclass_type.type.has_base(fullnames.MODEL_METACLASS_FULLNAME)


def is_registered_model_type(info: TypeInfo, django_context: "DjangoContext") -> bool:
return info.fullname in {get_class_fullname(cls) for cls in django_context.all_registered_model_classes}


def get_model_from_expression(
expr: Expression,
*,
Expand Down
5 changes: 4 additions & 1 deletion mypy_django_plugin/transformers/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ def transform_into_proper_return_type(ctx: FunctionContext, django_context: Djan
assert isinstance(default_return_type, Instance)

outer_model_info = helpers.get_typechecker_api(ctx).scope.active_class()
if outer_model_info is None or not helpers.is_model_type(outer_model_info):
if outer_model_info is None or (
not helpers.is_model_type(outer_model_info)
and not helpers.is_registered_model_type(outer_model_info, django_context)
):
return ctx.default_return_type

assert isinstance(outer_model_info, TypeInfo)
Expand Down
17 changes: 12 additions & 5 deletions tests/typecheck/models/test_3rd_party_models.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
- case: handles_type_annotations_on_3rd_party_models
installed_apps:
- django_extensions
- myapp
main: |
from django.db import models
from django_extensions.db.models import TimeStampedModel # type: ignore [import-untyped]
from myapp.models import A
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
from django_extensions.db.models import TimeStampedModel # type: ignore[import-untyped]
class A(TimeStampedModel):
name = models.CharField()
count = models.IntegerField()
class A(TimeStampedModel):
name = models.CharField()

0 comments on commit c200c4c

Please sign in to comment.