Skip to content

Commit

Permalink
Treat QuerySet.alias like QuerySet.annotate (#2411)
Browse files Browse the repository at this point in the history
Although this incorrectly allows some misuses of `alias` that only
work at runtime with `annotate`, that is better than incorrectly
forbidding all uses of `alias`.

Fixes #2302.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored Oct 21, 2024
1 parent 4dc5e5c commit b7f43dd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions mypy_django_plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def manager_and_queryset_method_hooks(self) -> Dict[str, Callable[[MethodContext
"values_list": partial(
querysets.extract_proper_type_queryset_values_list, django_context=self.django_context
),
"alias": partial(querysets.extract_proper_type_queryset_annotate, django_context=self.django_context),
"annotate": partial(querysets.extract_proper_type_queryset_annotate, django_context=self.django_context),
"create": partial(init_create.redefine_and_typecheck_model_create, django_context=self.django_context),
"filter": typecheck_filtering_method,
Expand Down
9 changes: 5 additions & 4 deletions tests/typecheck/managers/querysets/test_annotate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
qs.filter(annotate_wrong__gt=5)
qs2 = qs.alias(alias_entries=models.Count("entry"))
qs2.filter(alias_entries__gt=5)
qs2.filter(alias_wrong__gt=5, annotate_wrong__gt=5)
Blog.objects.annotate().filter(xyz=1)
Expand All @@ -378,10 +379,10 @@
)
out: |
main:6: error: Cannot resolve keyword 'annotate_wrong' into field. Choices are: entry, id, xyz [misc]
main:9: error: Cannot resolve keyword 'alias_wrong' into field. Choices are: entry, id, xyz [misc]
main:9: error: Cannot resolve keyword 'annotate_wrong' into field. Choices are: entry, id, xyz [misc]
main:11: error: Cannot resolve keyword 'xyz' into field. Choices are: entry, id [misc]
main:13: error: Cannot resolve keyword 'xyz' into field. Choices are: entry, id [misc]
main:10: error: Cannot resolve keyword 'alias_wrong' into field. Choices are: entry, id, xyz, alias_entries [misc]
main:10: error: Cannot resolve keyword 'annotate_wrong' into field. Choices are: entry, id, xyz, alias_entries [misc]
main:12: error: Cannot resolve keyword 'xyz' into field. Choices are: entry, id [misc]
main:14: error: Cannot resolve keyword 'xyz' into field. Choices are: entry, id [misc]
installed_apps:
- myapp
files:
Expand Down

0 comments on commit b7f43dd

Please sign in to comment.