Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow generic requests, responses, fields, views#8825

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@jalaziz
Copy link
Contributor

@jalazizjalaziz commentedJan 2, 2023
edited
Loading

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 inissues like this.

Viicos, Pixel-Jack, hanqitech, favll, and browniebroke reacted with thumbs up emoji
@lovelydinosaur
Copy link
Contributor

Can you explain why this is useful?

@jalaziz
Copy link
ContributorAuthor

jalaziz commentedJan 2, 2023
edited
Loading

@tomchristie I've updated the PR description with more details.

@jalaziz
Copy link
ContributorAuthor

jalaziz commentedJan 2, 2023
edited
Loading

To carry on the conversation from#7385. The types are being used to infer and check the types returned from methods like:get_queryset(),get_object(), etc. With it, we don't need to cast into the correct return types as it can be accurately inferred.

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.

@jalazizjalazizforce-pushed thejameel/type-checking-improvements branch 2 times, most recently fromc55dab3 to581fa59CompareJanuary 7, 2023 22:48
@jalaziz
Copy link
ContributorAuthor

Rebased to ensure the tests pass.

@auvipyauvipy requested a review fromrpkilbyJanuary 9, 2023 15:25
@melotte25
Copy link

Not to be a pain, but could another reviewer such as@auvipy take a look at this? We're upgrading to the latestmypy anddjangorestframework-stubs, and this would be great to get implemented to save a lot of repeat code (DRY!).

Thank you!

Pixel-Jack reacted with thumbs up emoji

@lovelydinosaur
Copy link
Contributor

By making these classes generic, type checkers can more easily do their job when working with DRF classes.

  • Could you show me a concrete type-checking issue that this change resolves?
  • Is this only useful together with thedjango-restframeworkstubs package?
  • Django doesn't appear to have adopted__class_getitem__ for it'sgeneric views or forQuerySets and Managers. I'd normally expect us to follow Django's lead on this kind of design issue. Is there any context we can see there involving similar design discussions against the Django project?
auvipy reacted with thumbs up emoji

@jalaziz
Copy link
ContributorAuthor

By making these classes generic, type checkers can more easily do their job when working with DRF classes.

  • Could you show me a concrete type-checking issue that this change resolves?

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.

  • Is this only useful together with thedjango-restframeworkstubs package?

Yes and no. Today,django-restframeworkstubs provides support for typing where DRF lacks typing support. There's nothing stopping another stubs implementation or for DRF to provide native typing support.

  • Django doesn't appear to have adopted__class_getitem__ for it'sgeneric views or forQuerySets and Managers. I'd normally expect us to follow Django's lead on this kind of design issue. Is there any context we can see there involving similar design discussions against the Django project?

Djangodoes support__class_getitem__ forQuerySets,Managers, andForeignKey. It's true that support hasn't been added for generic views, but that's just a matter of someone contributing support, I believe.

@jalaziz
Copy link
ContributorAuthor

@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 (despitepagination being set):

> curl -H 'Accept: application/json; indent=4' http://127.0.0.1:8000/users/[]

For this repo, the workaround is to move the monkey patching to after theREST_FRAMEWORK definition, but that doesn't work for more complicate setups with nested settings files.

@jalazizjalaziz changed the titleAllow generic requets, responses, fields, viewsAllow generic requests, responses, fields, viewsFeb 22, 2023
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 problematicas seen in this [issue][1].[1]:typeddjango/djangorestframework-stubs#299
@jalazizjalazizforce-pushed thejameel/type-checking-improvements branch from581fa59 toc066dfeCompareFebruary 22, 2023 04:42
Copy link
Contributor

@lovelydinosaurlovelydinosaur left a comment

Choose a reason for hiding this comment

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

Djangodoes support__class_getitem__ forQuerySets,Managers, andForeignKey.

This wins the case for you.

kavdev reacted with thumbs up emoji
@lovelydinosaur
Copy link
Contributor

Merging the latestmaster branch might resolve the failingpre-commit check...?

@auvipyauvipy added this to the3.15 milestoneFeb 22, 2023
@auvipyauvipy merged commit15c613a intoencode:masterFeb 22, 2023
@auvipy
Copy link
Collaborator

all green now! thanks

lovelydinosaur and Pixel-Jack reacted with thumbs up emoji

@auvipyauvipy removed the request for review fromrpkilbyFebruary 22, 2023 15:39
@auvipy
Copy link
Collaborator

Not to be a pain, but could another reviewer such as@auvipy take a look at this? We're upgrading to the latestmypy anddjangorestframework-stubs, and this would be great to get implemented to save a lot of repeat code (DRY!).

Thank you!

sorry I missed the notification

Pixel-Jack reacted with thumbs up emoji

@mschoettle
Copy link

@tomchristie I ran into the issue explained in the PR description. Are there any plans to make a release containing this fix?

pchabros reacted with thumbs up emoji

@mschoettle
Copy link

Or maybe@auvipy can answer the question about a release?

@auvipy
Copy link
Collaborator

Nope I can't. I don't have release access yet.

@pchabros
Copy link

It's still not released?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@lovelydinosaurlovelydinosaurlovelydinosaur approved these changes

@auvipyauvipyauvipy approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

3.15

Development

Successfully merging this pull request may close these issues.

6 participants

@jalaziz@lovelydinosaur@melotte25@auvipy@mschoettle@pchabros

[8]ページ先頭

©2009-2025 Movatter.jp