From f1ef30d409e6f9e9d4ac336646cc42d66630deb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Magimel?= Date: Tue, 7 May 2024 13:33:47 +0000 Subject: [PATCH 1/3] refactor(relations): reorder args in RelatedField --- rest_framework-stubs/relations.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rest_framework-stubs/relations.pyi b/rest_framework-stubs/relations.pyi index cf86940f5..46ae783d7 100644 --- a/rest_framework-stubs/relations.pyi +++ b/rest_framework-stubs/relations.pyi @@ -38,11 +38,11 @@ class RelatedField(Field[_MT, _DT, _PT, Any]): def __init__( self, *, - many: bool = ..., - allow_empty: bool = ..., queryset: QuerySet[_MT] | Manager[_MT] | None = ..., html_cutoff: int | None = ..., html_cutoff_text: str = ..., + many: bool = ..., + allow_empty: bool = ..., read_only: bool = ..., write_only: bool = ..., required: bool | None = None, @@ -51,10 +51,10 @@ class RelatedField(Field[_MT, _DT, _PT, Any]): source: str | None = None, label: StrOrPromise | None = ..., help_text: StrOrPromise | None = None, - allow_null: bool = ..., - validators: Sequence[Validator[_MT]] | None = ..., - error_messages: dict[str, StrOrPromise] | None = ..., style: dict[str, str] | None = ..., + error_messages: dict[str, StrOrPromise] | None = ..., + validators: Sequence[Validator[_MT]] | None = ..., + allow_null: bool = ..., ) -> None: ... # mypy doesn't accept the typing below, although its accurate to what this class is doing, hence the ignore def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _DT, _PT] | ManyRelatedField: ... # type: ignore @@ -62,13 +62,13 @@ class RelatedField(Field[_MT, _DT, _PT, Any]): def many_init(cls, *args: Any, **kwargs: Any) -> ManyRelatedField: ... def get_queryset(self) -> QuerySet[_MT]: ... def use_pk_only_optimization(self) -> bool: ... + def get_attribute(self, instance: _MT) -> _PT | None: ... def get_choices(self, cutoff: int | None = ...) -> dict: ... @property def choices(self) -> dict: ... @property def grouped_choices(self) -> dict: ... def iter_options(self) -> Iterable[Option]: ... - def get_attribute(self, instance: _MT) -> _PT | None: ... def display_value(self, instance: _MT) -> str: ... class StringRelatedField(RelatedField[_MT, _MT, str]): ... From 5ac16c123f68750b2328455dd11718330e74d941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Magimel?= Date: Tue, 7 May 2024 13:46:27 +0000 Subject: [PATCH 2/3] fix(relations): replace the data type TypeVar with Any Remove the data type TypeVar in favor of Any. Then subclasses don't have to deal with this rule anymore. Ref #168 --- rest_framework-stubs/relations.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rest_framework-stubs/relations.pyi b/rest_framework-stubs/relations.pyi index 46ae783d7..6c11e4fd6 100644 --- a/rest_framework-stubs/relations.pyi +++ b/rest_framework-stubs/relations.pyi @@ -31,7 +31,7 @@ _MT = TypeVar("_MT", bound=Model) _DT = TypeVar("_DT") # Data Type _PT = TypeVar("_PT") # Primitive Type -class RelatedField(Field[_MT, _DT, _PT, Any]): +class RelatedField(Field[_MT, Any, _PT, Any]): queryset: QuerySet[_MT] | Manager[_MT] | None html_cutoff: int | None html_cutoff_text: str | None @@ -57,7 +57,7 @@ class RelatedField(Field[_MT, _DT, _PT, Any]): allow_null: bool = ..., ) -> None: ... # mypy doesn't accept the typing below, although its accurate to what this class is doing, hence the ignore - def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _DT, _PT] | ManyRelatedField: ... # type: ignore + def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _PT] | ManyRelatedField: ... # type: ignore @classmethod def many_init(cls, *args: Any, **kwargs: Any) -> ManyRelatedField: ... def get_queryset(self) -> QuerySet[_MT]: ... @@ -71,9 +71,9 @@ class RelatedField(Field[_MT, _DT, _PT, Any]): def iter_options(self) -> Iterable[Option]: ... def display_value(self, instance: _MT) -> str: ... -class StringRelatedField(RelatedField[_MT, _MT, str]): ... +class StringRelatedField(RelatedField[_MT, str]): ... -class PrimaryKeyRelatedField(RelatedField[_MT, _MT, Any]): +class PrimaryKeyRelatedField(RelatedField[_MT, Any]): pk_field: str | None def __init__( self, @@ -98,7 +98,7 @@ class PrimaryKeyRelatedField(RelatedField[_MT, _MT, Any]): pk_field: str | Field | None = ..., ) -> None: ... -class HyperlinkedRelatedField(RelatedField[_MT, str, Hyperlink]): +class HyperlinkedRelatedField(RelatedField[_MT, Hyperlink]): reverse: Callable lookup_field: str lookup_url_kwarg: str @@ -134,7 +134,7 @@ class HyperlinkedRelatedField(RelatedField[_MT, str, Hyperlink]): class HyperlinkedIdentityField(HyperlinkedRelatedField): ... -class SlugRelatedField(RelatedField[_MT, str, str]): +class SlugRelatedField(RelatedField[_MT, str]): slug_field: str | None def __init__( self, From 3c65afa02c55f8004564d3002aef7210e2dbc4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Magimel?= Date: Tue, 7 May 2024 13:56:44 +0000 Subject: [PATCH 3/3] fix(relations): replace the model type TypeVar with Any Ref #168 --- rest_framework-stubs/relations.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rest_framework-stubs/relations.pyi b/rest_framework-stubs/relations.pyi index 6c11e4fd6..a77a472af 100644 --- a/rest_framework-stubs/relations.pyi +++ b/rest_framework-stubs/relations.pyi @@ -71,9 +71,9 @@ class RelatedField(Field[_MT, Any, _PT, Any]): def iter_options(self) -> Iterable[Option]: ... def display_value(self, instance: _MT) -> str: ... -class StringRelatedField(RelatedField[_MT, str]): ... +class StringRelatedField(RelatedField[Any, str]): ... -class PrimaryKeyRelatedField(RelatedField[_MT, Any]): +class PrimaryKeyRelatedField(RelatedField[Any, Any]): pk_field: str | None def __init__( self, @@ -98,7 +98,7 @@ class PrimaryKeyRelatedField(RelatedField[_MT, Any]): pk_field: str | Field | None = ..., ) -> None: ... -class HyperlinkedRelatedField(RelatedField[_MT, Hyperlink]): +class HyperlinkedRelatedField(RelatedField[Any, Hyperlink]): reverse: Callable lookup_field: str lookup_url_kwarg: str @@ -129,12 +129,12 @@ class HyperlinkedRelatedField(RelatedField[_MT, Hyperlink]): lookup_url_kwarg: str | None = ..., format: str | None = ..., ) -> None: ... - def get_object(self, view_name: str, view_args: list[Any], view_kwargs: dict[str, Any]) -> _MT: ... + def get_object(self, view_name: str, view_args: list[Any], view_kwargs: dict[str, Any]) -> Any: ... def get_url(self, obj: Model, view_name: str, request: Request, format: str | None) -> str | None: ... class HyperlinkedIdentityField(HyperlinkedRelatedField): ... -class SlugRelatedField(RelatedField[_MT, str]): +class SlugRelatedField(RelatedField[Any, str]): slug_field: str | None def __init__( self, @@ -158,7 +158,7 @@ class SlugRelatedField(RelatedField[_MT, str]): error_messages: dict[str, StrOrPromise] | None = ..., style: dict[str, str] | None = ..., ) -> None: ... - def to_internal_value(self, data: Any) -> _MT: ... + def to_internal_value(self, data: Any) -> Any: ... def to_representation(self, value: _MT) -> str: ... class ManyRelatedField(Field[Sequence[Any], Sequence[Any], list[Any], Any]):