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 filtering on implicit pk field on abstract model #1328

Merged
merged 2 commits into from
Jan 23, 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
4 changes: 4 additions & 0 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ def resolve_lookup_into_field(

def resolve_lookup_expected_type(self, ctx: MethodContext, model_cls: Type[Model], lookup: str) -> MypyType:
query = Query(model_cls)
if lookup == "pk" or lookup.startswith("pk__") and query.get_meta().pk is None:
# Primary key lookup when no primary key field is found, model is presumably
# abstract and we can't say anything about 'pk'.
return AnyType(TypeOfAny.implementation_artifact)
try:
lookup_parts, field_parts, is_expression = query.solve_lookup_type(lookup)
if is_expression:
Expand Down
9 changes: 9 additions & 0 deletions tests/typecheck/models/test_abstract.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- case: test_filter_on_abstract_user_pk
main: |
from django.contrib.auth.models import AbstractUser
AbstractUser.objects.get(pk=1)
AbstractUser.objects.get(pk__in=[1])
reveal_type(AbstractUser().pk) # N: Revealed type is "Any"
AbstractUser.objects.get(pkey=1) # ER: Cannot resolve keyword 'pkey' into field..*
installed_apps:
- django.contrib.auth