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

Handle repeated field lookups in calls to values_list #1441

Merged

Conversation

leamingrad
Copy link
Contributor

Prior to this change, if checked code was calling the values_list method
on a queryset with a repeated field lookup, for example
Model.objects.values_list("foo", "foo"), then we would fail to type the
resulting tuple correctly. The second argument would be untyped, and the
resulting tuple would have fewer fields than Django would actually give
it.

This problem only affects unnamed, non-flat tuples, as Django will error
if a repeating field lookups is attempted for these other variants.

This change ensures that when we type an unnamed tuple, we refer back to
the original field lookups when passing in the resolved values. We also
add a test to explain the issue, and prevent regressions.

Related issues

I could not find a related issue for this, but I can raise one if that
would be helpful.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks, just one proposal :)

content: |
from django.db import models
class MyUser(models.Model):
name = models.CharField(max_length=100)
Copy link
Member

Choose a reason for hiding this comment

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

Can we please have another field for this test? Something like age = models.IntegerField

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - good suggestion!


# Values tuples can have the same field repeated
values_tuple = MyUser.objects.values_list('name', 'name').get()
reveal_type(values_tuple) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
Copy link
Member

Choose a reason for hiding this comment

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

And let's use age here as well to be sure that this is not just a single type that repeats itself :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - good suggestion!. I've doubled-checked that without the fix the new version of the test does fail.

Prior to this change, if checked code was calling the values_list method
on a queryset with a repeated field lookup, for example
Model.objects.values_list("foo", "foo"), then we would fail to type the
resulting tuple correctly. The second argument would be untyped, and the
resulting tuple would have fewer fields than Django would actually give
it.

This problem only affects unnamed, non-flat tuples, as Django will error
if a repeating field lookups is attempted for these other variants.

This change ensures that when we type an unnamed tuple, we refer back to
the original field lookups when passing in the resolved values. We also
add a test to explain the issue, and prevent regressions.
@leamingrad leamingrad force-pushed the handle-repeated-values-list-entries branch from ba74bb3 to f2b30cb Compare April 15, 2023 08:28
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Great job! 👍

@sobolevn sobolevn merged commit 526c2c5 into typeddjango:master Apr 15, 2023
@leamingrad leamingrad deleted the handle-repeated-values-list-entries branch April 15, 2023 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants