-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Allow generic requests, responses, fields, views #8825
Allow generic requests, responses, fields, views #8825
Conversation
175ffd1
to
4c51b60
Compare
Can you explain why this is useful? |
@tomchristie I've updated the PR description with more details. |
To carry on the conversation from #7385. The types are being used to infer and check the types returned from methods like: Django has already shipped subscription for QuerySets and other classes which helps avoid the need for monkey patching, but DRF is still missing some key classes. |
c55dab3
to
581fa59
Compare
Rebased to ensure the tests pass. |
Not to be a pain, but could another reviewer such as @auvipy take a look at this? We're upgrading to the latest Thank you! |
|
It doesn't solve a type-checking issue per se, it solves a problem that occurs when you try to monkey-patch type support. Will try to put up an example project to show the issue, but fundamentally the issue is, unlike Django, DRF eagerly loads settings on import which results in improper settings when trying to monkey patch classes too early.
Yes and no. Today,
Django does support |
@tomchristie here's a test repo exhibiting some issues: https://github.com/jalaziz/drf-test-repo As it stands, we end up with no pagination because of the eager settings issue (despite pagination being set):
For this repo, the workaround is to move the monkey patching to after the |
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This allows the classes to be made generic for type checking. This is especially useful since monkey patching DRF can be problematic as seen in this [issue][1]. [1]: typeddjango/djangorestframework-stubs#299
581fa59
to
c066dfe
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Django does support
__class_getitem__
forQuerySets
, Managers, and ForeignKey.
This wins the case for you.
Merging the latest |
all green now! thanks |
sorry I missed the notification |
@tomchristie I ran into the issue explained in the PR description. Are there any plans to make a release containing this fix? |
Or maybe @auvipy can answer the question about a release? |
Nope I can't. I don't have release access yet. |
It's still not released? |
Description
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This is a follow up to #7385 and allows the classes to be made generic for type checking.
By making these classes generic, type checkers can more easily do their job when working with DRF classes. Today,
django-stubs and djangorestframework-stubs must monkey patch types to support generic typing, but the approach is problematic with DRF since DRF eagerly loads settings resulting in issues like this.