Browse Source

♻️ Cleanup more, remove unneeded code

pull/14857/head
Sebastián Ramírez 5 months ago
parent
commit
a150522d14
  1. 6
      fastapi/_compat/v2.py
  2. 6
      fastapi/dependencies/utils.py
  3. 7
      fastapi/routing.py

6
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]

6
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:

7
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(

Loading…
Cancel
Save