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

.values_list returns incorrect type for field with same name when selected from related model #2336

Open
flaeppe opened this issue Aug 10, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@flaeppe
Copy link
Member

flaeppe commented Aug 10, 2024

This wasn't a problem in 5.0.2 and thus I've bisected the issue to


If I run values_list and select a field on a related model, which happens to have the same name on the filtering model, types are not selected from the related model's field.

Repro case:

-   case: handles_field_with_same_name_on_other_model
    main: |
        from myapp.models import A
# Below reveals "Tuple[Union[builtins.str, None]]" which is from field `A.name` but `B.name` is selected in `values_list`
        reveal_type(A.objects.values_list("b__name").get())  # N: Revealed type is "Tuple[builtins.str]"
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class B(models.Model):
                    name = models.CharField()

                class A(models.Model):
                    b = models.ForeignKey(B, on_delete=models.CASCADE)
                    name = models.CharField(null=True)

What's wrong

Results in an error:

main:2: note: Revealed type is "Tuple[Union[builtins.str, None]]"

Above are the types from A.name but the query selects from B.name

How is that should be

main:2: note: Revealed type is "Tuple[builtins.str]"

@flaeppe flaeppe added the bug Something isn't working label Aug 10, 2024
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

No branches or pull requests

1 participant