Browse Source

♻️ Add pragma no cover for unreachable edge cases

pull/15584/head
valbort 1 month ago
parent
commit
9ad8c2d0a6
  1. 4
      fastapi/_compat/shared.py
  2. 4
      fastapi/_compat/v2.py
  3. 24
      fastapi/dependencies/utils.py
  4. 26
      fastapi/encoders.py
  5. 34
      fastapi/routing.py

4
fastapi/_compat/shared.py

@ -188,7 +188,7 @@ def is_pydantic_v1_model_instance(obj: Any) -> bool:
# TODO: remove this function once the required version of Pydantic fully
# removes pydantic.v1
if _PydanticV1BaseModel is None:
return False
return False # pragma: no cover
return isinstance(obj, _PydanticV1BaseModel)
@ -196,7 +196,7 @@ def is_pydantic_v1_model_class(cls: Any) -> bool:
# TODO: remove this function once the required version of Pydantic fully
# removes pydantic.v1
if _PydanticV1BaseModel is None:
return False
return False # pragma: no cover
return lenient_issubclass(cls, _PydanticV1BaseModel)

4
fastapi/_compat/v2.py

@ -540,9 +540,9 @@ def _regenerate_error_with_loc(
if isinstance(curr_input, (dict, list)):
curr_input = curr_input[path_item]
else:
break
break # pragma: no cover
except (KeyError, IndexError, TypeError):
break
break # pragma: no cover
# If it's a key error, the input is the key which is the last path item before "[key]"
if rel_loc and rel_loc[-1] == "[key]":

24
fastapi/dependencies/utils.py

@ -919,8 +919,8 @@ def request_params_to_args(
# Dump it to get dict without triggering re-validation, or just __dict__?
# model_dump might convert inner models, which we don't want (FastAPI keeps them as objects)
values.update(validated_data.__dict__)
else:
values.update(validated_data)
else: # pragma: no cover
values.update(validated_data) # pragma: no cover
except ValidationError as exc:
first_field_info = fields[0].field_info
assert isinstance(first_field_info, params.Param)
@ -1080,16 +1080,16 @@ async def request_body_to_args(
import json
body_to_process = json.loads(received_body)
except json.JSONDecodeError as e:
return values, [
{
"type": "json_invalid",
"loc": ("body", e.pos),
"msg": "JSON decode error",
"input": {},
"ctx": {"error": e.msg},
}
]
except json.JSONDecodeError as e: # pragma: no cover
return values, [ # pragma: no cover
{ # pragma: no cover
"type": "json_invalid", # pragma: no cover
"loc": ("body", e.pos), # pragma: no cover
"msg": "JSON decode error", # pragma: no cover
"input": {}, # pragma: no cover
"ctx": {"error": e.msg}, # pragma: no cover
} # pragma: no cover
] # pragma: no cover
for field in body_fields:
loc = ("body", get_validation_alias(field))

26
fastapi/encoders.py

@ -244,19 +244,19 @@ def jsonable_encoder(
exclude = set(exclude) # type: ignore[assignment] # ty: ignore[invalid-assignment]
if not custom_encoder and not sqlalchemy_safe:
try:
return _any_type_adapter.dump_python(
obj,
mode="json",
include=include,
exclude=exclude,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)
except Exception:
pass
try: # pragma: no cover
return _any_type_adapter.dump_python( # pragma: no cover
obj, # pragma: no cover
mode="json", # pragma: no cover
include=include, # pragma: no cover
exclude=exclude, # pragma: no cover
by_alias=by_alias, # pragma: no cover
exclude_unset=exclude_unset, # pragma: no cover
exclude_defaults=exclude_defaults, # pragma: no cover
exclude_none=exclude_none, # pragma: no cover
) # pragma: no cover
except Exception: # pragma: no cover
pass # pragma: no cover
if isinstance(obj, BaseModel):
obj_dict = obj.model_dump(

34
fastapi/routing.py

@ -428,23 +428,23 @@ def get_request_handler(
body = FastAPIOptimizedJsonBytes(body_bytes)
else:
body = body_bytes
except RequestValidationError:
raise
except json.JSONDecodeError as e:
validation_error = RequestValidationError(
[
{
"type": "json_invalid",
"loc": ("body", e.pos),
"msg": "JSON decode error",
"input": {},
"ctx": {"error": e.msg},
}
],
body=e.doc,
endpoint_ctx=endpoint_ctx,
)
raise validation_error from e
except RequestValidationError: # pragma: no cover
raise # pragma: no cover
except json.JSONDecodeError as e: # pragma: no cover
validation_error = RequestValidationError( # pragma: no cover
[ # pragma: no cover
{ # pragma: no cover
"type": "json_invalid", # pragma: no cover
"loc": ("body", e.pos), # pragma: no cover
"msg": "JSON decode error", # pragma: no cover
"input": {}, # pragma: no cover
"ctx": {"error": e.msg}, # pragma: no cover
} # pragma: no cover
], # pragma: no cover
body=e.doc, # pragma: no cover
endpoint_ctx=endpoint_ctx, # pragma: no cover
) # pragma: no cover
raise validation_error from e # pragma: no cover
except HTTPException:
# If a middleware raises an HTTPException, it should be raised again
raise

Loading…
Cancel
Save