From 338a33e2b9eecb9138d29fa470634b279c5956d0 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 6 May 2024 18:26:36 -0400 Subject: [PATCH] ValidationException.errors() are ErrorDetails Update the documentation to explain that `RequestValidationError` isn't literally a subclass since Pydantic V2. --- docs/en/docs/tutorial/handling-errors.md | 2 +- fastapi/_compat.py | 27 +++++++++++++++--------- fastapi/dependencies/utils.py | 26 ++++++++++++----------- fastapi/exceptions.py | 25 ++++++++++++++++------ fastapi/routing.py | 2 +- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/docs/en/docs/tutorial/handling-errors.md b/docs/en/docs/tutorial/handling-errors.md index 98ac55d1f7722..175820be8e0ab 100644 --- a/docs/en/docs/tutorial/handling-errors.md +++ b/docs/en/docs/tutorial/handling-errors.md @@ -163,7 +163,7 @@ path -> item_id !!! warning These are technical details that you might skip if it's not important for you now. -`RequestValidationError` is a sub-class of Pydantic's `ValidationError`. +`RequestValidationError` is morally a sub-class of Pydantic's `ValidationError`. **FastAPI** uses it so that, if you use a Pydantic model in `response_model`, and your data has an error, you will see the error in your log. diff --git a/fastapi/_compat.py b/fastapi/_compat.py index ad1410158fbd0..f9deb1c820087 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -17,12 +17,12 @@ Union, ) -from fastapi.exceptions import RequestErrorModel +from fastapi.exceptions import ErrorDetails, RequestErrorModel from fastapi.types import IncEx, ModelNameMap, UnionType from pydantic import BaseModel, create_model from pydantic.version import VERSION as P_VERSION from starlette.datastructures import UploadFile -from typing_extensions import Annotated, Literal, get_args, get_origin +from typing_extensions import Annotated, Literal, TypeAlias, get_args, get_origin # Reassign variable to make it reexported for mypy PYDANTIC_VERSION = P_VERSION @@ -82,6 +82,9 @@ class BaseConfig: class ErrorWrapper(Exception): pass + # See https://github.com/pydantic/pydantic/blob/07b6473/pydantic/v1/error_wrappers.py#L45-L47 + ErrorList: TypeAlias = Union[Sequence[Any], ErrorWrapper] + @dataclass class ModelField: field_info: FieldInfo @@ -115,13 +118,14 @@ def get_default(self) -> Any: return Undefined return self.field_info.get_default(call_default_factory=True) + # See https://github.com/pydantic/pydantic/blob/07b6473/pydantic/v1/fields.py#L850-L852 for the signature. def validate( self, value: Any, values: Dict[str, Any] = {}, # noqa: B006 *, loc: Tuple[Union[int, str], ...] = (), - ) -> Tuple[Any, Union[List[Dict[str, Any]], None]]: + ) -> Tuple[Any, Union[ErrorList, None]]: try: return ( self._type_adapter.validate_python(value, from_attributes=True), @@ -167,7 +171,7 @@ def get_annotation_from_field_info( ) -> Any: return annotation - def _normalize_errors(errors: Sequence[Any]) -> List[Dict[str, Any]]: + def _normalize_errors(errors: Sequence[Any]) -> List[ErrorDetails]: return errors # type: ignore[return-value] def _model_rebuild(model: Type[BaseModel]) -> None: @@ -265,12 +269,12 @@ def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]: assert issubclass(origin_type, sequence_types) # type: ignore[arg-type] return sequence_annotation_to_type[origin_type](value) # type: ignore[no-any-return] - def get_missing_field_error(loc: Tuple[str, ...]) -> Dict[str, Any]: + def get_missing_field_error(loc: Tuple[str, ...]) -> ErrorDetails: error = ValidationError.from_exception_data( "Field required", [{"type": "missing", "loc": loc, "input": {}}] ).errors(include_url=False)[0] error["input"] = None - return error # type: ignore[return-value] + return error def create_body_model( *, fields: Sequence[ModelField], model_name: str @@ -289,6 +293,9 @@ def create_body_model( from pydantic.class_validators import ( # type: ignore[no-redef] Validator as Validator, ) + from pydantic.error_wrappers import ( # type: ignore[no-redef] + ErrorList as ErrorList, + ) from pydantic.error_wrappers import ( # type: ignore[no-redef] ErrorWrapper as ErrorWrapper, ) @@ -418,7 +425,7 @@ def is_pv1_scalar_sequence_field(field: ModelField) -> bool: return True return False - def _normalize_errors(errors: Sequence[Any]) -> List[Dict[str, Any]]: + def _normalize_errors(errors: Sequence[Any]) -> List[ErrorDetails]: use_errors: List[Any] = [] for error in errors: if isinstance(error, ErrorWrapper): @@ -500,10 +507,10 @@ def copy_field_info(*, field_info: FieldInfo, annotation: Any) -> FieldInfo: def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]: return sequence_shape_to_type[field.shape](value) # type: ignore[no-any-return,attr-defined] - def get_missing_field_error(loc: Tuple[str, ...]) -> Dict[str, Any]: + def get_missing_field_error(loc: Tuple[str, ...]) -> ErrorDetails: missing_field_error = ErrorWrapper(MissingError(), loc=loc) # type: ignore[call-arg] new_error = ValidationError([missing_field_error], RequestErrorModel) - return new_error.errors()[0] # type: ignore[return-value] + return new_error.errors()[0] def create_body_model( *, fields: Sequence[ModelField], model_name: str @@ -516,7 +523,7 @@ def create_body_model( def _regenerate_error_with_loc( *, errors: Sequence[Any], loc_prefix: Tuple[Union[str, int], ...] -) -> List[Dict[str, Any]]: +) -> List[ErrorDetails]: updated_loc_errors: List[Any] = [ {**err, "loc": loc_prefix + err.get("loc", ())} for err in _normalize_errors(errors) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 4f984177a4085..1d9f37bb2311e 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -50,6 +50,7 @@ contextmanager_in_threadpool, ) from fastapi.dependencies.models import Dependant, SecurityRequirement +from fastapi.exceptions import ErrorDetails from fastapi.logger import logger from fastapi.security.base import SecurityBase from fastapi.security.oauth2 import OAuth2, SecurityScopes @@ -539,7 +540,7 @@ async def solve_dependencies( Dict[Tuple[Callable[..., Any], Tuple[str]], Any], ]: values: Dict[str, Any] = {} - errors: List[Any] = [] + errors: List[ErrorDetails] = [] if response is None: response = Response() del response.headers["content-length"] @@ -652,9 +653,9 @@ async def solve_dependencies( def request_params_to_args( required_params: Sequence[ModelField], received_params: Union[Mapping[str, Any], QueryParams, Headers], -) -> Tuple[Dict[str, Any], List[Any]]: +) -> Tuple[Dict[str, Any], List[ErrorDetails]]: values = {} - errors = [] + errors: List[ErrorDetails] = [] for field in required_params: if is_scalar_sequence_field(field) and isinstance( received_params, (QueryParams, Headers) @@ -675,10 +676,9 @@ def request_params_to_args( continue v_, errors_ = field.validate(value, values, loc=loc) if isinstance(errors_, ErrorWrapper): - errors.append(errors_) + errors.extend(_regenerate_error_with_loc(errors=[errors_], loc_prefix=())) elif isinstance(errors_, list): - new_errors = _regenerate_error_with_loc(errors=errors_, loc_prefix=()) - errors.extend(new_errors) + errors.extend(_regenerate_error_with_loc(errors=errors_, loc_prefix=())) else: values[field.name] = v_ return values, errors @@ -687,9 +687,9 @@ def request_params_to_args( async def request_body_to_args( required_params: List[ModelField], received_body: Optional[Union[Dict[str, Any], FormData]], -) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]: +) -> Tuple[Dict[str, Any], List[ErrorDetails]]: values = {} - errors: List[Dict[str, Any]] = [] + errors: List[ErrorDetails] = [] if required_params: field = required_params[0] field_info = field.field_info @@ -757,10 +757,12 @@ async def process_fn( v_, errors_ = field.validate(value, values, loc=loc) - if isinstance(errors_, list): - errors.extend(errors_) - elif errors_: - errors.append(errors_) + if isinstance(errors_, ErrorWrapper): + errors.extend( + _regenerate_error_with_loc(errors=[errors_], loc_prefix=()) + ) + elif isinstance(errors_, list): + errors.extend(_regenerate_error_with_loc(errors=errors_, loc_prefix=())) else: values[field.name] = v_ return values, errors diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index 44d4ada86d7e4..dd4de78d7d424 100644 --- a/fastapi/exceptions.py +++ b/fastapi/exceptions.py @@ -1,9 +1,22 @@ -from typing import Any, Dict, Optional, Sequence, Type, Union +from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union from pydantic import BaseModel, create_model from starlette.exceptions import HTTPException as StarletteHTTPException from starlette.exceptions import WebSocketException as StarletteWebSocketException -from typing_extensions import Annotated, Doc +from typing_extensions import Annotated, Doc, TypedDict + + +class ErrorDetails(TypedDict): + """ + The common subset shared by `ErrorDict` in Pydantic V1[0] and `ErrorDetails` in Pydantic V2[1]. + + [0] https://github.com/pydantic/pydantic/blob/4d7bef62aeff10985fe67d13477fe666b13ba070/pydantic/v1/error_wrappers.py#L21-L22 + [1] https://github.com/pydantic/pydantic-core/blob/e1fc99dd3207157aad77defc20ab6873fd268b5b/python/pydantic_core/__init__.py#L73-L91 + """ + + loc: Tuple[Union[int, str], ...] + msg: str + type: str class HTTPException(StarletteHTTPException): @@ -147,15 +160,15 @@ class FastAPIError(RuntimeError): class ValidationException(Exception): - def __init__(self, errors: Sequence[Any]) -> None: + def __init__(self, errors: Sequence[ErrorDetails]) -> None: self._errors = errors - def errors(self) -> Sequence[Any]: + def errors(self) -> Sequence[ErrorDetails]: return self._errors class RequestValidationError(ValidationException): - def __init__(self, errors: Sequence[Any], *, body: Any = None) -> None: + def __init__(self, errors: Sequence[ErrorDetails], *, body: Any = None) -> None: super().__init__(errors) self.body = body @@ -165,7 +178,7 @@ class WebSocketRequestValidationError(ValidationException): class ResponseValidationError(ValidationException): - def __init__(self, errors: Sequence[Any], *, body: Any = None) -> None: + def __init__(self, errors: Sequence[ErrorDetails], *, body: Any = None) -> None: super().__init__(errors) self.body = body diff --git a/fastapi/routing.py b/fastapi/routing.py index fa1351859fb91..a5e0929156d48 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -251,7 +251,7 @@ async def app(request: Request) -> Response: "msg": "JSON decode error", "input": {}, "ctx": {"error": e.msg}, - } + } # type: ignore [typeddict-unknown-key] ], body=e.doc, )