Skip to content

Fix PageNumberPagination.paginate_queryset() return type #165

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

Conversation

richardxia
Copy link
Contributor

It looks like the return type for PageNumberPagination.paginate_queryset() was incorrectly set to Optional[List[Page]], when it should really be an optional list of the QuerySet's underlying model type. The actual implementation in DRF shows that it returns list(self.page), which transforms the page to a list, not wrapping it with a list, since Page is itself a sequence of model objects. I have confirmed this by running actual code on djangorestframework 3.12 and Django 3.2 and confirming that I'm getting a list of model instances, not a list of pages of model instances.

The type stubs for the other two Pagination classes here look like they have the correct return type, which provide a parameter _MT for the QuerySet argument and uses that parameter in the return type, so this change really just brings PageNumberPagination up to date with the other two.

I also added a type checking test for this, although I'm not sure how useful it is, since it's pretty trivial. Let me know if you'd prefer that the test be removed.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Note that this has nothing to do with the change to the
paginate_queryset() type signature.
@richardxia
Copy link
Contributor Author

I believe the typechecking test that is failing doesn't have anything to do with my changes here. I diff'd the console output between my failing CI build here and the latest succeeding build in master from 23 days ago, and the only difference appears to be the following:

drf_source/tests/test_fields.py:2016: error: Argument 1 to "ListField" has incompatible type "CharField"; expected "bool"

I tried running the type checking tests on my local checkout, and even on the master branch, that test fails for me. I took a look at the django-rest-framework codebase itself, and it looks like that was a brand new test that was merged 20 days ago in encode/django-rest-framework#7632.

I pushed up a new commit to add a new exclusion for that test, but let me know if you prefer me to submit a separate PR for that, since it's technically unrelated to any of my changes here.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks again!

@sobolevn sobolevn merged commit 5293c1f into typeddjango:master Aug 27, 2021
@richardxia richardxia deleted the fix-page-number-pagination-paginate-queryset-return-type branch August 27, 2021 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants