Skip to content

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

Closed
@flaeppe

Description

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]"

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions