Skip to content

Commit

Permalink
Fix crash when FK references unknown 'app_label' (#1342)
Browse files Browse the repository at this point in the history
Plugin previously crashed running the provided test case, now it yields an intended error message instead.
  • Loading branch information
flaeppe authored Jan 25, 2023
1 parent 832163d commit e6d65d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ def get_field_related_model_cls(
related_model_fullname = field.model.__module__ + "." + related_model_cls
related_model_cls = self.get_model_class_by_fullname(related_model_fullname)
else:
related_model_cls = self.apps_registry.get_model(related_model_cls)
try:
related_model_cls = self.apps_registry.get_model(related_model_cls)
except LookupError:
return None

return related_model_cls

Expand Down
15 changes: 15 additions & 0 deletions tests/typecheck/fields/test_related.yml
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,18 @@
class SalesMan(models.Model):
client = models.ManyToManyField(CustomUser)
- case: test_fails_if_app_label_is_unknown_in_relation_field
main: |
from installed.models import InstalledModel
installed_apps:
- installed
files:
- path: installed/__init__.py
- path: installed/models.py
content: |
from django.db import models
class InstalledModel(models.Model):
non_installed = models.ForeignKey( # E: Cannot find model 'not_installed.NonInstalledModel' referenced in field 'non_installed'
"not_installed.NonInstalledModel", on_delete=models.CASCADE
)

0 comments on commit e6d65d2

Please sign in to comment.