-
-
Notifications
You must be signed in to change notification settings - Fork 458
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
Handle repeated field lookups in calls to values_list #1441
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]" |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
ba74bb3
to
f2b30cb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! 👍
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.