Skip to content

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

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

Closed
LucidDan opened this issue Mar 31, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@LucidDan
Copy link

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 LucidDan added the bug Something isn't working label Mar 31, 2022
@flaeppe
Copy link
Member

flaeppe commented Jul 30, 2024

I'm going to bring in a quote from Django's documentation regarding object_list on ListView:

While this view is executing, self.object_list will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.

Ref: https://docs.djangoproject.com/en/5.0/ref/class-based-views/generic-display/#django.views.generic.list.ListView

The type of object_list is no longer Sequence, since #909. I'm not totally sure if the current type is optimal and the quote from the docs doesn't really help either, but it's currently better than Sequence.

@flaeppe flaeppe closed this as completed Jul 30, 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

2 participants