Skip to content

Consider changing _BaseQuerySet to QuerySet #551

Closed
@davidhalter

Description

@davidhalter

Bug report

What's wrong

A Jedi user realized that some things about Django stubs did not add up: davidhalter/jedi#1667.

I realized that the reason for this is that _BaseQuerySet does not exist in Django's code base. This is probably a "hack" to work with proper types so that mypy can deal with Django's dynamic nature.

However this leads to frameworks like Jedi (and probably others) not properly infer types from Python to stubs and back.

How is that should be

I'm not sure what the best solution is for this issue. Maybe we can get rid of _BaseQuerySet entirely by using something like class ValuesQuerySet(QuerySet[T], Collection[_Row]), but I'm not sure that's possible.

Another way forward could be to move most of the methods like filter onto QuerySet and duplicate them on ValuesQuerySet.

The best solution would probably be to make QuerySet[T, Row]. This would mean that QuerySet has two type generics. This is great, because this really maps to what a QuerySet actually is in Django. Of course this has the drawback that everywhere where we have QuerySet[T] now, we have to write it like QuerySet[T, T]. But then again this wouldn't affect much:

$ git grep QuerySet\\[ django-stubs/ | wc -l
18

System information

  • OS: Ubuntu/Windows/Mac
  • python version: 3.6 - 3.10
  • django version: 3
  • mypy version: -
  • django-stubs version: 3d2534e

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