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

Allow lookups on .annotate fields #2376

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
12 changes: 6 additions & 6 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,12 @@ def resolve_lookup_expected_type(
try:
solved_lookup = self.solve_lookup_type(model_cls, lookup)
except FieldError as exc:
if (
helpers.is_annotated_model(model_instance.type)
and model_instance.extra_attrs
and lookup in model_instance.extra_attrs.attrs
):
return model_instance.extra_attrs.attrs[lookup]
if helpers.is_annotated_model(model_instance.type) and model_instance.extra_attrs:
# If the field comes from .annotate(), we assume Any for it
# and allow chaining any lookups.
lookup_base_field, *_ = lookup.split("__")
if lookup_base_field in model_instance.extra_attrs.attrs:
return model_instance.extra_attrs.attrs[lookup_base_field]

msg = exc.args[0]
if model_instance.extra_attrs:
Expand Down
26 changes: 25 additions & 1 deletion tests/typecheck/managers/querysets/test_annotate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
username = models.CharField(max_length=100)


- case: annotate_currently_allows_lookups_of_non_existent_field
- case: annotate_rejects_lookups_of_non_existent_field
main: |
from myapp.models import User
from django.db.models.expressions import F
Expand Down Expand Up @@ -395,3 +395,27 @@

class Entry(models.Model):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)

- case: test_annotate_allows_any_lookups_in_filter
main: |
from django.db import models
from myapp.models import Blog

qs = Blog.objects.annotate(distance=0)
reveal_type(qs)
reveal_type(qs.filter(distance=10))
reveal_type(qs.filter(distance__lt=10))
qs.filter(distance__lt__lt=10)
qs.filter(distance__unknown_lookup=10)
out: |
main:5: note: Revealed type is "django.db.models.query.QuerySet[myapp.models.Blog@AnnotatedWith[TypedDict({'distance': Any})], myapp.models.Blog@AnnotatedWith[TypedDict({'distance': Any})]]"
main:6: note: Revealed type is "django.db.models.query.QuerySet[myapp.models.Blog@AnnotatedWith[TypedDict({'distance': Any})], myapp.models.Blog@AnnotatedWith[TypedDict({'distance': Any})]]"
main:7: note: Revealed type is "django.db.models.query.QuerySet[myapp.models.Blog@AnnotatedWith[TypedDict({'distance': Any})], myapp.models.Blog@AnnotatedWith[TypedDict({'distance': Any})]]"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class Blog(models.Model): pass
Loading