Skip to content

Question regarding typing a values_list with (flat=True) #1047

Closed
@rvanlaar

Description

@rvanlaar

Bug report

What's wrong

I can't set the correct type on the return value of a values list.
Simplified example:

class Question:
    category = models.TextField()
def get_result() -> QuerySet[Any]:
    result = Exam.objects.get().questions.values_list("category", flat=True)
    reveal_type(result) # Revealed type is "django.db.models.query._QuerySet[backend.models.Question, Union[builtins.str, None]]"
    return result
reveal_type(get_result()) # Revealed type is "django.db.models.query._QuerySet[Any, Any]"

How is that should be

The calling code needs to loop over the categories, i.e. it needs a sequence of type str.
What is the best way to annotate the get_result function?

System information

  • OS: debian 11
  • python version: 3.9.7
  • django version: 3.2.13
  • mypy version: 0.961
  • django-stubs version: 1.12.0
  • django-stubs-ext version: 0.4.0

Metadata

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