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

[5.0.3 regression] Can’t filter a QuerySet on an alias #2302

Open
andersk opened this issue Jul 31, 2024 · 4 comments · May be fixed by #2411
Open

[5.0.3 regression] Can’t filter a QuerySet on an alias #2302

andersk opened this issue Jul 31, 2024 · 4 comments · May be fixed by #2411
Labels
bug Something isn't working

Comments

@andersk
Copy link
Contributor

andersk commented Jul 31, 2024

from django.db import models

class Blog(models.Model):
    pass

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

def test() -> None:
    Blog.objects.alias(entries=models.Count("entry")).filter(entries__gt=5)
my_app/models.py:13: error: Cannot resolve keyword 'entries' into field. Choices are: entry, id  [misc]
Found 1 error in 1 file (checked 3 source files)

This was introduced by a28717d (#2277).

@andersk andersk added the bug Something isn't working label Jul 31, 2024
@flaeppe
Copy link
Member

flaeppe commented Jul 31, 2024

I don't know if we should call this a regression? Any version prior to 5.0.3 just didn't try to check the filter call.

At least there's no plugin code that tries to do anything with alias

@andersk
Copy link
Contributor Author

andersk commented Jul 31, 2024

From a user perspective, this was allowed in 5.0.2 and incorrectly disallowed in 5.0.3. Whatever you’d like to call it, we can’t upgrade to django-stubs ≥ 5.0.3 in Zulip until it’s addressed.

@sobolevn
Copy link
Member

sobolevn commented Aug 1, 2024

Really interesting: there are no handlers for alias in

@cached_property
def manager_and_queryset_method_hooks(self) -> Dict[str, Callable[[MethodContext], MypyType]]:
typecheck_filtering_method = partial(orm_lookups.typecheck_queryset_filter, django_context=self.django_context)
return {
"values": partial(querysets.extract_proper_type_queryset_values, django_context=self.django_context),
"values_list": partial(
querysets.extract_proper_type_queryset_values_list, 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,
"get": typecheck_filtering_method,
"exclude": typecheck_filtering_method,
}

I will start digging into this!

@sobolevn
Copy link
Member

sobolevn commented Aug 1, 2024

The problem happens because we use entries__gt=5 in filter, which should be allowed. We need to add alias() support and infer field names from there.

sobolevn added a commit that referenced this issue Aug 1, 2024
andersk added a commit to andersk/django-stubs that referenced this issue Oct 20, 2024
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 typeddjango#2302.

Signed-off-by: Anders Kaseorg <[email protected]>
andersk added a commit to andersk/django-stubs that referenced this issue Oct 20, 2024
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 typeddjango#2302.

Signed-off-by: Anders Kaseorg <[email protected]>
@andersk andersk linked a pull request Oct 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

3 participants