diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 63abea325..a92492c0d 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -264,7 +264,9 @@ if PYDANTIC_V2: origin_type = ( get_origin(field.field_info.annotation) or field.field_info.annotation ) - if not issubclass(origin_type, sequence_types) or not isinstance(origin_type, type): + if not issubclass(origin_type, sequence_types) or not isinstance( + origin_type, type + ): raise TypeError(f"Field {field.name} is not a supported sequence type") return sequence_annotation_to_type[origin_type](value) # type: ignore[no-any-return] diff --git a/fastapi/applications.py b/fastapi/applications.py index d88188368..e2bd3cd24 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -875,7 +875,9 @@ class FastAPI(Starlette): if not self.title: raise ValueError("A title must be provided for OpenAPI, e.g.: 'My API'") if not self.version: - raise ValueError("A version must be provided for OpenAPI, e.g.: '2.1.0'") + raise ValueError( + "A version must be provided for OpenAPI, e.g.: '2.1.0'" + ) # TODO: remove when discarding the openapi_prefix parameter if openapi_prefix: logger.warning( diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index e2f0c838e..5aee38600 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -108,7 +108,9 @@ def ensure_multipart_is_installed() -> None: try: # __version__ is available in both multiparts, and can be mocked - from multipart import __version__ as multipart_version # type: ignore[no-redef,import-untyped] + from multipart import ( + __version__ as multipart_version, # type: ignore[no-redef,import-untyped] + ) except ImportError: logger.error(multipart_not_installed_error) raise RuntimeError(multipart_not_installed_error) from None @@ -118,7 +120,9 @@ def ensure_multipart_is_installed() -> None: raise RuntimeError(multipart_incorrect_install_error) try: - from multipart.multipart import parse_options_header # type: ignore[import-untyped] + from multipart.multipart import ( + parse_options_header, # type: ignore[import-untyped] + ) except ImportError: logger.error(multipart_incorrect_install_error) raise RuntimeError(multipart_incorrect_install_error) from None @@ -132,7 +136,9 @@ def get_param_sub_dependant( security_scopes: Optional[List[str]] = None, ) -> Dependant: if depends.dependency is None: - raise ValueError(f"`depends.dependency` must be set for parameter '{param_name}'") + raise ValueError( + f"`depends.dependency` must be set for parameter '{param_name}'" + ) return get_sub_dependant( depends=depends, @@ -314,7 +320,9 @@ def get_dependant( dependant=dependant, ): if param_details.field is not None: - raise ValueError(f"Cannot specify multiple FastAPI annotations for {param_name!r}") + raise ValueError( + f"Cannot specify multiple FastAPI annotations for {param_name!r}" + ) continue @@ -400,7 +408,10 @@ def analyze_param( field_info=fastapi_annotation, annotation=use_annotation ) - if field_info.default is not Undefined and field_info.default is not RequiredParam: + if ( + field_info.default is not Undefined + and field_info.default is not RequiredParam + ): raise ValueError( f"`{field_info.__class__.__name__}` default value cannot be set in " f"`Annotated` for {param_name!r}. Set the default value with `=` instead." @@ -464,7 +475,9 @@ def analyze_param( raise ValueError(f"Cannot specify `Depends` for type {type_annotation!r}") if field_info is not None: - raise ValueError(f"Cannot specify FastAPI annotation for type {type_annotation!r}") + raise ValueError( + f"Cannot specify FastAPI annotation for type {type_annotation!r}" + ) # Handle default assignations, neither field_info nor depends was not found in Annotated nor default value elif field_info is None and depends is None: @@ -520,16 +533,18 @@ def analyze_param( ) if is_path_param: if not is_scalar_field(field=field): - raise TypeError("Path parameters must be of one of the supported scalar types") + raise TypeError( + "Path parameters must be of one of the supported scalar types" + ) elif isinstance(field_info, params.Query): if not ( is_scalar_field(field) or is_scalar_sequence_field(field) or ( - isinstance(field.type_, type) and - issubclass(field.type_, BaseModel) and - getattr(field, "shape", 1) == 1 # shape check for Pydantic v1 + isinstance(field.type_, type) + and issubclass(field.type_, BaseModel) + and getattr(field, "shape", 1) == 1 # shape check for Pydantic v1 ) ): raise TypeError( @@ -894,7 +909,9 @@ async def _extract_form_body( ): # For types if not isinstance(value, sequence_types): - raise TypeError(f"Expected a sequence type (e.g., list, tuple), got {type(value).__name__}") + raise TypeError( + f"Expected a sequence type (e.g., list, tuple), got {type(value).__name__}" + ) results: List[Union[bytes, str]] = [] @@ -925,7 +942,9 @@ async def request_body_to_args( errors: List[Dict[str, Any]] = [] if not body_fields: - raise ValueError("request_body_to_args() should be called with at least one field") + raise ValueError( + "request_body_to_args() should be called with at least one field" + ) single_not_embedded_field = len(body_fields) == 1 and not embed_body_fields first_field = body_fields[0] diff --git a/fastapi/openapi/models.py b/fastapi/openapi/models.py index d41c38981..5b7327e91 100644 --- a/fastapi/openapi/models.py +++ b/fastapi/openapi/models.py @@ -15,7 +15,7 @@ from typing_extensions import Annotated, Literal, TypedDict from typing_extensions import deprecated as typing_deprecated try: - import email_validator # noqa: F401 + import email_validator # noqa: F401 from pydantic import EmailStr except ImportError: # pragma: no cover diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 925826c9e..9e78d1596 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -181,7 +181,9 @@ def get_openapi_operation_request_body( return None if not isinstance(body_field, ModelField): - raise TypeError(f"Expected body_field to be a ModelField, got {type(body_field).__name__}") + raise TypeError( + f"Expected body_field to be a ModelField, got {type(body_field).__name__}" + ) body_schema = get_schema_from_model_field( field=body_field, @@ -466,7 +468,9 @@ def get_fields_from_routes( ): if route.body_field: if not isinstance(route.body_field, ModelField): - raise TypeError("A request body must be a Pydantic ModelField instance") + raise TypeError( + "A request body must be a Pydantic ModelField instance" + ) body_fields_from_routes.append(route.body_field) if route.response_field: diff --git a/fastapi/responses.py b/fastapi/responses.py index 32067c683..6cb52bba9 100644 --- a/fastapi/responses.py +++ b/fastapi/responses.py @@ -1,13 +1,7 @@ from typing import Any from starlette.responses import ( - FileResponse, - HTMLResponse, JSONResponse, - PlainTextResponse, - RedirectResponse, - Response, - StreamingResponse, ) # noqa try: diff --git a/fastapi/routing.py b/fastapi/routing.py index cf61bc049..16d78ee6b 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -380,7 +380,7 @@ def get_websocket_app( if solved_result.errors: raise WebSocketRequestValidationError( _normalize_errors(solved_result.errors) - ) + ) if dependant.call is None: raise ValueError("dependant.call must be a callable function") await dependant.call(**solved_result.values) @@ -508,7 +508,9 @@ class APIRoute(routing.Route): self.status_code = status_code if self.response_model: if not is_body_allowed_for_status_code(status_code): - raise ValueError(f"Status code {status_code} must not have a response body") + raise ValueError( + f"Status code {status_code} must not have a response body" + ) response_name = "Response_" + self.unique_id self.response_field = create_model_field( name=response_name, @@ -541,7 +543,9 @@ class APIRoute(routing.Route): model = response.get("model") if model: if not is_body_allowed_for_status_code(status_code): - raise ValueError(f"Status code {status_code} must not have a response body") + raise ValueError( + f"Status code {status_code} must not have a response body" + ) response_name = f"Response_{additional_status_code}_{self.unique_id}" response_field = create_model_field( name=response_name, type_=model, mode="serialization" @@ -850,7 +854,9 @@ class APIRouter(routing.Router): if not prefix.startswith("/"): raise ValueError("A path prefix must start with '/'") if prefix.endswith("/"): - raise ValueError("A path prefix must not end with '/', as the routes will start with '/'") + raise ValueError( + "A path prefix must not end with '/', as the routes will start with '/'" + ) self.prefix = prefix self.tags: List[Union[str, Enum]] = tags or [] @@ -1263,7 +1269,9 @@ class APIRouter(routing.Router): if not prefix.startswith("/"): raise ValueError("A path prefix must start with '/'") if prefix.endswith("/"): - raise ValueError("A path prefix must not end with '/', as the routes will start with '/'") + raise ValueError( + "A path prefix must not end with '/', as the routes will start with '/'" + ) else: for r in router.routes: