django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

BaseListView.object_list should not (only?) be a Sequence, as it is set to a QuerySet in the default CBV

Open LucidDan opened this issue 3 years ago • 0 comments

Bug report

What's wrong

The recent addition of Sequence for BaseListView.object_list breaks code that expects it to be a QuerySet (which is the default behaviour in Django CBVs, unless you override the queryset attribute or get_queryset method).

A commit adding a failing test case can be found here: https://github.com/LucidDan/django-stubs/commit/7ec7761708653439945a589d08d98e6174e7c6b9

How is that should be

At a minimum, object_list should be a Union of Iterable[T] or a QuerySet[T].

However, I don't think this is an optimal solution either. The default behaviour in Django CBVs is that it be a QuerySet - BaseListView.get() sets the value of object_list to the result of get_queryset(), which currently is always a QuerySet[T] in django-stubs (as is the attribute MultipleObjectMixin.queryset). So it is a bit inconsistent to have object_list be typed differently.

As per #477 , QuerySet is over-specific for get_queryset() (and for the queryset attribute) and probably should be possible for it to be an Iterable[T] as well. My concern on that issue applies here too.

As I asked in the other issue; is there a better solution? This is one area of python typing I find really challenging, and I'm not sure what options there are to make this work in a good way, if any.

System information

  • OS:
  • python version: 3.9.10 and 3.10.2
  • django version: 4.0.2
  • mypy version: 0.942
  • django-stubs version: 1.10.1
  • django-stubs-ext version: 0.4.0

LucidDan avatar Mar 31 '22 02:03 LucidDan