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

Fix crash when FK references unknown 'app_label' #1342

Merged
merged 1 commit into from
Jan 25, 2023
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
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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it also possible to ignore this error using a # type: ignore comment?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and indeed it works.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same. Yet the test passes successfully with the following diff:

diff --git a/tests/typecheck/fields/test_related.yml b/tests/typecheck/fields/test_related.yml
index 7e9758ae..cc863373 100644
--- a/tests/typecheck/fields/test_related.yml
+++ b/tests/typecheck/fields/test_related.yml
@@ -940,6 +940,6 @@
             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'
+                    non_installed = models.ForeignKey(  # type: ignore
                         "not_installed.NonInstalledModel", on_delete=models.CASCADE
                     )

I'm not complaining 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How interesting, have to compare how this error is emitted versus the one that couldn't be ignored

"not_installed.NonInstalledModel", on_delete=models.CASCADE
)