Skip to content

Commit

Permalink
Extend return type of ListView.get_queryset
Browse files Browse the repository at this point in the history
Which can return either a `QuerySet` or an iterable. See, e.g.
https://github.com/django/django/blob/83c803f161044fbfbfcd9a0c94ca93dc131be662/django/views/generic/list.py#L26-L27

This has always been true for the generic `ListView` class.
  • Loading branch information
codeinthehole committed Mar 29, 2022
1 parent 4e3f9d6 commit 0fb447e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions django-stubs/views/generic/list.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Generic, Optional, Sequence, Tuple, Type, TypeVar
from typing import Any, Generic, Optional, Sequence, Tuple, Type, TypeVar, Union, Iterable

from django.core.paginator import Paginator
from django.db.models import Model
Expand All @@ -18,7 +18,7 @@ class MultipleObjectMixin(Generic[T], ContextMixin):
paginator_class: Type[Paginator] = ...
page_kwarg: str = ...
ordering: Sequence[str] = ...
def get_queryset(self) -> QuerySet[T]: ...
def get_queryset(self) -> Union[QuerySet[T], Iterable[T]]: ...
def get_ordering(self) -> Sequence[str]: ...
def paginate_queryset(self, queryset: QuerySet, page_size: int) -> Tuple[Paginator, int, QuerySet[T], bool]: ...
def get_paginate_by(self, queryset: QuerySet) -> Optional[int]: ...
Expand Down
25 changes: 24 additions & 1 deletion tests/typecheck/views/generic/test_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@
class MyModel(models.Model):
...
- case: nonqueryset_list_view
main: |
from typing import List
from django.views.generic import ListView
from django.db.models import QuerySet
from myapp.models import MyModel
class MyListView(ListView):
model = MyModel
def get_queryset(self) -> List[MyModel]:
...
custom_settings: |
INSTALLED_APPS = ('myapp',)
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyModel(models.Model):
...
- case: generic_list_view_wrong
main: |
Expand Down Expand Up @@ -49,4 +72,4 @@
out: |
main:7: error: Incompatible types in assignment (expression has type "Type[MyModel]", base class "MultipleObjectMixin" defined the type as "Optional[Type[Other]]")
main:8: error: Incompatible types in assignment (expression has type "_QuerySet[MyModel, MyModel]", base class "MultipleObjectMixin" defined the type as "Optional[_QuerySet[Other, Other]]")
main:10: error: Return type "_QuerySet[MyModel, MyModel]" of "get_queryset" incompatible with return type "_QuerySet[Other, Other]" in supertype "MultipleObjectMixin"
main:10: error: Return type "_QuerySet[MyModel, MyModel]" of "get_queryset" incompatible with return type "Iterable[Other]" in supertype "MultipleObjectMixin"

0 comments on commit 0fb447e

Please sign in to comment.