Skip to content

Add accepted_renderer, accepted_media_type to Request and Response #649

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

Merged
merged 2 commits into from
Sep 11, 2024
Merged
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
3 changes: 3 additions & 0 deletions rest_framework-stubs/request.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from rest_framework.authentication import BaseAuthentication
from rest_framework.authtoken.models import Token
from rest_framework.negotiation import BaseContentNegotiation
from rest_framework.parsers import BaseParser
from rest_framework.renderers import BaseRenderer
from rest_framework.versioning import BaseVersioning
from rest_framework.views import APIView
from typing_extensions import Self
Expand Down Expand Up @@ -47,6 +48,8 @@ class Request(HttpRequest):
parsers: Sequence[BaseParser] | None
authenticators: Sequence[BaseAuthentication | ForcedAuthentication] | None
negotiator: BaseContentNegotiation | None
accepted_renderer: BaseRenderer | None
accepted_media_type: str | None
Comment on lines +51 to +52
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these can actually be None? Looking at APIView.perform_content_negotiation(), it rather raises exception than returns None.

Same for parse_header_parameters(), it raises NotAcceptable if it can't find a match.

But still this PR is an improvement, I'll merge this and get a release out the door. We can improve this later.

Copy link
Contributor Author

@q0w q0w Sep 12, 2024

Choose a reason for hiding this comment

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

but request may not have these attrs at all. There are hasattr checks after all

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, there's no way to annotate 'possibly missing attribute' for classes. A code comment in pyi is best we can do.

But that's different from None. If the value may be missing but never None then I'd suggest to remove | None. Because that's even more misleading.

parser_context: dict[str, Any]
version: str | None
versioning_scheme: BaseVersioning | None
Expand Down
3 changes: 3 additions & 0 deletions rest_framework-stubs/response.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from django.template.base import Template
from django.template.response import SimpleTemplateResponse
from django.test.utils import ContextList
from django.urls import ResolverMatch
from rest_framework.renderers import BaseRenderer
from rest_framework.request import Request
from rest_framework.test import APIClient
from typing_extensions import Self
Expand All @@ -13,6 +14,8 @@ class Response(SimpleTemplateResponse):
data: Any
exception: bool
content_type: str | None
accepted_renderer: BaseRenderer | None
accepted_media_type: str | None
_request: Request
def __init__(
self,
Expand Down