Skip to content

Commit 5293c1f

Browse files
authored
Fix PageNumberPagination.paginate_queryset() return type (#165)
* PageNumberPagination.paginate_queryset() should return a List of the underlying QuerySet's model, not Page. * Add typecheck test for PageNumberPagination.paginate_queryset(). * Add typecheck test exclusion for new test in DRF. Note that this has nothing to do with the change to the paginate_queryset() type signature.
1 parent 8ba53c7 commit 5293c1f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

rest_framework-stubs/pagination.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class PageNumberPagination(BasePagination):
6363
request: Optional[Request] = ...
6464
template: str = ...
6565
def paginate_queryset(
66-
self, queryset: QuerySet, request: Request, view: Optional[APIView] = ...
67-
) -> Optional[List[Page]]: ...
66+
self, queryset: QuerySet[_MT], request: Request, view: Optional[APIView] = ...
67+
) -> Optional[List[_MT]]: ...
6868
def get_paginated_response_schema(self, schema: Dict[str, Any]) -> Dict[str, Any]: ...
6969
def get_schema_fields(self, view: APIView) -> List[CoreAPIField]: ...
7070
def get_schema_operation_parameters(self, view: APIView) -> List[Dict[str, Any]]: ...

scripts/typecheck_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
'"MultipleChoiceField[Model]" has no attribute "partial"',
106106
'Argument 1 to "to_internal_value" of "Field" has incompatible type "Dict[str, str]"; expected "List[Any]"',
107107
'Module "rest_framework.fields" has no attribute "DjangoImageField"; maybe "ImageField"?',
108+
'Argument 1 to "ListField" has incompatible type "CharField"; expected "bool"',
108109
],
109110
"test_filters.py": [
110111
'Module has no attribute "coreapi"',

tests/typecheck/test_pagination.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- case: test_page_number_pagination_paginate_queryset
2+
main: |
3+
from rest_framework.pagination import PageNumberPagination
4+
from rest_framework.request import Request
5+
from django.contrib.auth.models import User
6+
from django.db.models import QuerySet
7+
8+
paginator = PageNumberPagination()
9+
request: Request
10+
queryset: QuerySet[User]
11+
page = paginator.paginate_queryset(queryset, request)
12+
reveal_type(page) # N: Revealed type is "Union[builtins.list[django.contrib.auth.models.User*], None]"

0 commit comments

Comments
 (0)