Skip to content
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

[3.15] Add __class_getitem__ of classes #585

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions rest_framework-stubs/fields.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import sys
import uuid
from _typeshed import Incomplete
from collections.abc import Callable, Generator, Iterable, Mapping, Sequence
Expand All @@ -17,6 +18,9 @@ from rest_framework.serializers import BaseSerializer
from rest_framework.validators import Validator
from typing_extensions import Self, TypeAlias

if sys.version_info >= (3, 9):
from types import GenericAlias

logger: Logger

class _Empty(Enum):
Expand Down Expand Up @@ -105,6 +109,9 @@ class Field(Generic[_VT, _DT, _RP, _IN]):
validators: Sequence[Validator[_VT]] | None = ...,
allow_null: bool = ...,
) -> None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, *args: Any, **kwargs: Any) -> GenericAlias: ...

def bind(self, field_name: str, parent: BaseSerializer) -> None: ...
@property
def validators(self) -> list[Validator[_VT]]: ...
Expand Down
7 changes: 7 additions & 0 deletions rest_framework-stubs/generics.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from _typeshed import Incomplete
from collections.abc import Sequence
from typing import Any, Protocol, TypeVar
Expand All @@ -11,6 +12,9 @@ from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import BaseSerializer

if sys.version_info >= (3, 9):
from types import GenericAlias

_MT_co = TypeVar("_MT_co", bound=Model, covariant=True)
_MT_inv = TypeVar("_MT_inv", bound=Model)

Expand All @@ -37,6 +41,9 @@ class GenericAPIView(views.APIView, UsesQuerySet[_MT_co]):
lookup_url_kwarg: str | None
filter_backends: Sequence[type[BaseFilterBackend | BaseFilterProtocol[_MT_co]]]
pagination_class: type[BasePagination] | None
if sys.version_info >= (3, 9):
JaeHyuckSa marked this conversation as resolved.
Show resolved Hide resolved
def __class_getitem__(cls, *args: Any, **kwargs: Any) -> GenericAlias: ...

def get_object(self) -> _MT_co: ...
def get_serializer(self, *args: Any, **kwargs: Any) -> BaseSerializer[_MT_co]: ...
def get_serializer_class(self) -> type[BaseSerializer[_MT_co]]: ...
Expand Down
7 changes: 7 additions & 0 deletions rest_framework-stubs/request.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from _typeshed import Incomplete
from collections.abc import Iterator, Sequence
from contextlib import AbstractContextManager, contextmanager
Expand All @@ -15,6 +16,9 @@ from rest_framework.parsers import BaseParser
from rest_framework.versioning import BaseVersioning
from rest_framework.views import APIView

if sys.version_info >= (3, 9):
from types import GenericAlias

def is_form_media_type(media_type: str) -> bool: ...

class override_method(AbstractContextManager[Request]):
Expand Down Expand Up @@ -58,6 +62,9 @@ class Request(HttpRequest):
negotiator: BaseContentNegotiation | None = ...,
parser_context: dict[str, Any] | None = ...,
) -> None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, *args: Any, **kwargs: Any) -> GenericAlias: ...

@property
def content_type(self) -> str: ... # type: ignore[override]
@property
Expand Down
7 changes: 7 additions & 0 deletions rest_framework-stubs/response.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from collections.abc import Mapping
from typing import Any

Expand All @@ -8,6 +9,9 @@ from django.urls import ResolverMatch
from rest_framework.request import Request
from rest_framework.test import APIClient

if sys.version_info >= (3, 9):
from types import GenericAlias

class Response(SimpleTemplateResponse):
data: Any
exception: bool
Expand All @@ -22,6 +26,9 @@ class Response(SimpleTemplateResponse):
exception: bool = ...,
content_type: str | None = ...,
) -> None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, *args: Any, **kwargs: Any) -> GenericAlias: ...

@property
def rendered_content(self) -> Any: ...
def render(self) -> Any: ...
Expand Down
5 changes: 0 additions & 5 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,8 @@ rest_framework.views.AsView
rest_framework.views.GenericView

# Added in 3.15:
rest_framework.fields.Field.__class_getitem__
rest_framework.filters.search_smart_split
rest_framework.generics.GenericAPIView.__class_getitem__
rest_framework.request.Request.__class_getitem__
rest_framework.response.Response.__class_getitem__
rest_framework.routers.BaseRouter.is_already_registered
rest_framework.serializers.Field.__class_getitem__
rest_framework.serializers.ListSerializer.run_child_validation
rest_framework.serializers.ModelSerializer.get_unique_together_constraints
rest_framework.templatetags.rest_framework.optional_logout
Expand Down