diff --git a/fastapi/_compat/shared.py b/fastapi/_compat/shared.py index 4df520444e..20cc62177f 100644 --- a/fastapi/_compat/shared.py +++ b/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) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 1b86d55678..6cd003347a 100644 --- a/fastapi/_compat/v2.py +++ b/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]": diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index a9193ebd8b..891d18458c 100644 --- a/fastapi/dependencies/utils.py +++ b/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)) diff --git a/fastapi/encoders.py b/fastapi/encoders.py index 435056b710..e558a646fc 100644 --- a/fastapi/encoders.py +++ b/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( diff --git a/fastapi/routing.py b/fastapi/routing.py index f3c8ebb7d9..88d63472a4 100644 --- a/fastapi/routing.py +++ b/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