Skip to content
Open
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
30 changes: 25 additions & 5 deletions rest_framework-stubs/validators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class ContextValidator(Protocol[_V]):
requires_context: bool
def __call__(self, value: _V, context: Field, /) -> None: ...

Validator: TypeAlias = Callable[[_V], None] | ContextValidator[_V]
Validator: TypeAlias = (
Callable[[_V], None] | ContextValidator[_V] | UniqueValidator | UniqueTogetherValidator | BaseUniqueForValidator
Copy link
Contributor

Choose a reason for hiding this comment

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

This change shouldn't be necessary.

But this was quite tricky to figure out, it would help if it was explained in a comment.

And the Validator type alias would be better called FieldValidator.

Copy link
Contributor

Choose a reason for hiding this comment

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

As I understand, what was asked in the original issue

was actually different. The Serializer class inherits a validators property from Field class (@property def validators(self) -> list[Validator[_VT]] from Field class.

What it doesn't account for is that in case of Serializer class, validators in that case actually can include UniqueTogetherValidator and others.

)

def qs_exists(queryset: QuerySet) -> bool: ...
def qs_filter(queryset: QuerySet[_T], **kwargs: Any) -> QuerySet[_T]: ...
Expand All @@ -35,10 +37,18 @@ class UniqueTogetherValidator:
requires_context: bool
queryset: QuerySet
fields: Iterable[str]
def __init__(self, queryset: QuerySet, fields: Iterable[str], message: StrOrPromise | None = ...) -> None: ...
def __init__(
self,
queryset: QuerySet,
fields: Iterable[str],
message: StrOrPromise | None = ...,
) -> None: ...
def enforce_required_fields(self, attrs: Container[str], serializer: BaseSerializer) -> None: ...
def filter_queryset(
self, attrs: MutableMapping[str, Any], queryset: QuerySet[_T], serializer: BaseSerializer
self,
attrs: MutableMapping[str, Any],
queryset: QuerySet[_T],
serializer: BaseSerializer,
) -> QuerySet[_T]: ...
def exclude_current_instance(
self, attrs: MutableMapping[str, Any], queryset: QuerySet[_T], instance: _T
Expand All @@ -57,10 +67,20 @@ class BaseUniqueForValidator:
queryset: QuerySet
field: str
date_field: str
def __init__(self, queryset: QuerySet, field: str, date_field: str, message: StrOrPromise | None = ...) -> None: ...
def __init__(
self,
queryset: QuerySet,
field: str,
date_field: str,
message: StrOrPromise | None = ...,
) -> None: ...
def enforce_required_fields(self, attrs: Container[str]) -> None: ...
def filter_queryset(
self, attrs: MutableMapping[str, Any], queryset: QuerySet[_T], field_name: str, date_field_name: str
self,
attrs: MutableMapping[str, Any],
queryset: QuerySet[_T],
field_name: str,
date_field_name: str,
) -> QuerySet[_T]: ...
def exclude_current_instance(
self, attrs: MutableMapping[str, Any], queryset: QuerySet[_T], instance: Model
Expand Down