Skip to content
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

Extend return type of django.views.generic.list.ListView.get_queryset to allow any iterable #897

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"