From a150522d14d52d6721236ae3d391b4ae0b8a936b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 6 Feb 2026 19:58:07 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Cleanup=20more,=20remove?= =?UTF-8?q?=20unneeded=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/_compat/v2.py | 6 +++--- fastapi/dependencies/utils.py | 6 +----- fastapi/routing.py | 7 ++----- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 766976a974..57b3d94ffc 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -169,11 +169,11 @@ class ModelField: values: dict[str, Any] = {}, # noqa: B006 *, loc: tuple[Union[int, str], ...] = (), - ) -> tuple[Any, Union[list[dict[str, Any]], None]]: + ) -> tuple[Any, list[dict[str, Any]]]: try: return ( self._type_adapter.validate_python(value, from_attributes=True), - None, + [], ) except ValidationError as exc: return None, _regenerate_error_with_loc( @@ -359,7 +359,7 @@ def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]: return shared.sequence_annotation_to_type[origin_type](value) # type: ignore[no-any-return,index] -def get_missing_field_error(loc: tuple[str, ...]) -> dict[str, Any]: +def get_missing_field_error(loc: tuple[Union[int, str], ...]) -> dict[str, Any]: error = ValidationError.from_exception_data( "Field required", [{"type": "missing", "loc": loc, "input": {}}] ).errors(include_url=False)[0] diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 7ea0dee6e6..dd42371ecc 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -717,11 +717,7 @@ def _validate_value_with_model_field( return None, [get_missing_field_error(loc=loc)] else: return deepcopy(field.default), [] - v_, errors_ = field.validate(value, values, loc=loc) - if isinstance(errors_, list): - return None, errors_ - else: - return v_, [] + return field.validate(value, values, loc=loc) def _is_json_field(field: ModelField) -> bool: diff --git a/fastapi/routing.py b/fastapi/routing.py index b8d1f76248..fe8d886093 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -277,15 +277,12 @@ async def serialize_response( endpoint_ctx: Optional[EndpointContext] = None, ) -> Any: if field: - errors = [] if is_coroutine: - value, errors_ = field.validate(response_content, {}, loc=("response",)) + value, errors = field.validate(response_content, {}, loc=("response",)) else: - value, errors_ = await run_in_threadpool( + value, errors = await run_in_threadpool( field.validate, response_content, {}, loc=("response",) ) - if isinstance(errors_, list): - errors.extend(errors_) if errors: ctx = endpoint_ctx or EndpointContext() raise ResponseValidationError(