From 3223de5598310359262ff3e3a0259a00851cbc58 Mon Sep 17 00:00:00 2001 From: "Nima Mashhadi M. Reza" <49960770+MashhadiNima@users.noreply.github.com> Date: Mon, 3 Aug 2020 16:59:07 +0430 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Add=20typing.Optional=20to=20var?= =?UTF-8?q?iables=20that=20accept=20None=20as=20value=20=20(#1731)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nimashadix Co-authored-by: Sebastián Ramírez --- fastapi/applications.py | 282 +++++++++--------- fastapi/dependencies/models.py | 38 +-- fastapi/dependencies/utils.py | 27 +- fastapi/encoders.py | 6 +- fastapi/exceptions.py | 7 +- fastapi/openapi/utils.py | 2 +- fastapi/param_functions.py | 157 +++++----- fastapi/params.py | 180 ++++++------ fastapi/routing.py | 324 ++++++++++----------- fastapi/security/api_key.py | 12 +- fastapi/security/http.py | 14 +- fastapi/security/oauth2.py | 16 +- fastapi/security/open_id_connect_url.py | 6 +- fastapi/utils.py | 4 +- tests/test_serialize_response.py | 2 +- tests/test_serialize_response_dataclass.py | 2 +- tests/test_serialize_response_model.py | 2 +- tests/test_validate_response.py | 2 +- tests/test_validate_response_dataclass.py | 2 +- 19 files changed, 558 insertions(+), 527 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index ab895d657..95f6e26ee 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -32,7 +32,7 @@ class FastAPI(Starlette): self, *, debug: bool = False, - routes: List[BaseRoute] = None, + routes: Optional[List[BaseRoute]] = None, title: str = "FastAPI", description: str = "", version: str = "0.1.0", @@ -44,10 +44,12 @@ class FastAPI(Starlette): redoc_url: Optional[str] = "/redoc", swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect", swagger_ui_init_oauth: Optional[dict] = None, - middleware: Sequence[Middleware] = None, - exception_handlers: Dict[Union[int, Type[Exception]], Callable] = None, - on_startup: Sequence[Callable] = None, - on_shutdown: Sequence[Callable] = None, + middleware: Optional[Sequence[Middleware]] = None, + exception_handlers: Optional[ + Dict[Union[int, Type[Exception]], Callable] + ] = None, + on_startup: Optional[Sequence[Callable]] = None, + on_shutdown: Optional[Sequence[Callable]] = None, openapi_prefix: str = "", root_path: str = "", root_path_in_servers: bool = True, @@ -187,27 +189,27 @@ class FastAPI(Starlette): path: str, endpoint: Callable, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - methods: List[str] = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + methods: Optional[List[str]] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, ) -> None: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -242,27 +244,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - methods: List[str] = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + methods: Optional[List[str]] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -299,11 +301,11 @@ class FastAPI(Starlette): return decorator def add_api_websocket_route( - self, path: str, endpoint: Callable, name: str = None + self, path: str, endpoint: Callable, name: Optional[str] = None ) -> None: self.router.add_api_websocket_route(path, endpoint, name=name) - def websocket(self, path: str, name: str = None) -> Callable: + def websocket(self, path: str, name: Optional[str] = None) -> Callable: def decorator(func: Callable) -> Callable: self.add_api_websocket_route(path, func, name=name) return func @@ -315,9 +317,9 @@ class FastAPI(Starlette): router: routing.APIRouter, *, prefix: str = "", - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - responses: Dict[Union[int, str], Dict[str, Any]] = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, default_response_class: Optional[Type[Response]] = None, ) -> None: self.router.include_router( @@ -334,27 +336,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -388,27 +390,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -442,27 +444,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -496,27 +498,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -550,27 +552,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -604,27 +606,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -658,27 +660,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -712,27 +714,27 @@ class FastAPI(Starlette): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[routing.APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[routing.APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover diff --git a/fastapi/dependencies/models.py b/fastapi/dependencies/models.py index 5fe99f3b4..586852211 100644 --- a/fastapi/dependencies/models.py +++ b/fastapi/dependencies/models.py @@ -1,4 +1,4 @@ -from typing import Callable, List, Sequence +from typing import Callable, List, Optional, Sequence from fastapi.security.base import SecurityBase @@ -12,7 +12,9 @@ param_supported_types = (str, int, float, bool) class SecurityRequirement: - def __init__(self, security_scheme: SecurityBase, scopes: Sequence[str] = None): + def __init__( + self, security_scheme: SecurityBase, scopes: Optional[Sequence[str]] = None + ): self.security_scheme = security_scheme self.scopes = scopes @@ -21,23 +23,23 @@ class Dependant: def __init__( self, *, - path_params: List[ModelField] = None, - query_params: List[ModelField] = None, - header_params: List[ModelField] = None, - cookie_params: List[ModelField] = None, - body_params: List[ModelField] = None, - dependencies: List["Dependant"] = None, - security_schemes: List[SecurityRequirement] = None, - name: str = None, - call: Callable = None, - request_param_name: str = None, - websocket_param_name: str = None, - response_param_name: str = None, - background_tasks_param_name: str = None, - security_scopes_param_name: str = None, - security_scopes: List[str] = None, + path_params: Optional[List[ModelField]] = None, + query_params: Optional[List[ModelField]] = None, + header_params: Optional[List[ModelField]] = None, + cookie_params: Optional[List[ModelField]] = None, + body_params: Optional[List[ModelField]] = None, + dependencies: Optional[List["Dependant"]] = None, + security_schemes: Optional[List[SecurityRequirement]] = None, + name: Optional[str] = None, + call: Optional[Callable] = None, + request_param_name: Optional[str] = None, + websocket_param_name: Optional[str] = None, + response_param_name: Optional[str] = None, + background_tasks_param_name: Optional[str] = None, + security_scopes_param_name: Optional[str] = None, + security_scopes: Optional[List[str]] = None, use_cache: bool = True, - path: str = None, + path: Optional[str] = None, ) -> None: self.path_params = path_params or [] self.query_params = query_params or [] diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 1526547bf..2c70b2c10 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -97,7 +97,7 @@ sequence_shape_to_type = { def get_param_sub_dependant( - *, param: inspect.Parameter, path: str, security_scopes: List[str] = None + *, param: inspect.Parameter, path: str, security_scopes: Optional[List[str]] = None ) -> Dependant: depends: params.Depends = param.default if depends.dependency: @@ -125,8 +125,8 @@ def get_sub_dependant( depends: params.Depends, dependency: Callable, path: str, - name: str = None, - security_scopes: List[str] = None, + name: Optional[str] = None, + security_scopes: Optional[List[str]] = None, ) -> Dependant: security_requirement = None security_scopes = security_scopes or [] @@ -157,7 +157,10 @@ CacheKey = Tuple[Optional[Callable], Tuple[str, ...]] def get_flat_dependant( - dependant: Dependant, *, skip_repeats: bool = False, visited: List[CacheKey] = None + dependant: Dependant, + *, + skip_repeats: bool = False, + visited: Optional[List[CacheKey]] = None, ) -> Dependant: if visited is None: visited = [] @@ -269,8 +272,8 @@ def get_dependant( *, path: str, call: Callable, - name: str = None, - security_scopes: List[str] = None, + name: Optional[str] = None, + security_scopes: Optional[List[str]] = None, use_cache: bool = True, ) -> Dependant: path_param_names = get_path_param_names(path) @@ -348,7 +351,7 @@ def get_param_field( param: inspect.Parameter, param_name: str, default_field_info: Type[params.Param] = params.Param, - force_type: params.ParamTypes = None, + force_type: Optional[params.ParamTypes] = None, ignore_default: bool = False, ) -> ModelField: default_value = Required @@ -456,10 +459,10 @@ async def solve_dependencies( request: Union[Request, WebSocket], dependant: Dependant, body: Optional[Union[Dict[str, Any], FormData]] = None, - background_tasks: BackgroundTasks = None, - response: Response = None, - dependency_overrides_provider: Any = None, - dependency_cache: Dict[Tuple[Callable, Tuple[str]], Any] = None, + background_tasks: Optional[BackgroundTasks] = None, + response: Optional[Response] = None, + dependency_overrides_provider: Optional[Any] = None, + dependency_cache: Optional[Dict[Tuple[Callable, Tuple[str]], Any]] = None, ) -> Tuple[ Dict[str, Any], List[ErrorWrapper], @@ -653,7 +656,7 @@ async def request_body_to_args( else: loc = ("body", field.alias) - value: Any = None + value: Optional[Any] = None if received_body is not None: if ( field.shape in sequence_shapes or field.type_ in sequence_types diff --git a/fastapi/encoders.py b/fastapi/encoders.py index 3f5b79d9e..bf85fff81 100644 --- a/fastapi/encoders.py +++ b/fastapi/encoders.py @@ -1,7 +1,7 @@ from enum import Enum from pathlib import PurePath from types import GeneratorType -from typing import Any, Callable, Dict, List, Set, Tuple, Union +from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union from fastapi.logger import logger from fastapi.utils import PYDANTIC_1 @@ -29,10 +29,10 @@ encoders_by_class_tuples = generate_encoders_by_class_tuples(ENCODERS_BY_TYPE) def jsonable_encoder( obj: Any, - include: Union[SetIntStr, DictIntStrAny] = None, + include: Optional[Union[SetIntStr, DictIntStrAny]] = None, exclude: Union[SetIntStr, DictIntStrAny] = set(), by_alias: bool = True, - skip_defaults: bool = None, + skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index be196d0cb..d7da2030f 100644 --- a/fastapi/exceptions.py +++ b/fastapi/exceptions.py @@ -1,4 +1,4 @@ -from typing import Any, Sequence +from typing import Any, Dict, Optional, Sequence from fastapi.utils import PYDANTIC_1 from pydantic import ValidationError, create_model @@ -10,7 +10,10 @@ from starlette.websockets import WebSocket class HTTPException(StarletteHTTPException): def __init__( - self, status_code: int, detail: Any = None, headers: dict = None + self, + status_code: int, + detail: Any = None, + headers: Optional[Dict[str, Any]] = None, ) -> None: super().__init__(status_code=status_code, detail=detail) self.headers = headers diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 1cf79d71e..c5e38e993 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -329,7 +329,7 @@ def get_openapi( title: str, version: str, openapi_version: str = "3.0.2", - description: str = None, + description: Optional[str] = None, routes: Sequence[BaseRoute], tags: Optional[List[Dict[str, Any]]] = None, servers: Optional[List[Dict[str, Union[str, Any]]]] = None, diff --git a/fastapi/param_functions.py b/fastapi/param_functions.py index abd95609c..91620c7c0 100644 --- a/fastapi/param_functions.py +++ b/fastapi/param_functions.py @@ -1,4 +1,4 @@ -from typing import Any, Callable, Sequence +from typing import Any, Callable, Optional, Sequence from fastapi import params @@ -6,17 +6,17 @@ from fastapi import params def Path( # noqa: N802 default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ) -> Any: return params.Path( @@ -39,17 +39,17 @@ def Path( # noqa: N802 def Query( # noqa: N802 default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ) -> Any: return params.Query( @@ -72,18 +72,18 @@ def Query( # noqa: N802 def Header( # noqa: N802 default: Any, *, - alias: str = None, + alias: Optional[str] = None, convert_underscores: bool = True, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ) -> Any: return params.Header( @@ -107,17 +107,17 @@ def Header( # noqa: N802 def Cookie( # noqa: N802 default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ) -> Any: return params.Cookie( @@ -142,16 +142,16 @@ def Body( # noqa: N802 *, embed: bool = False, media_type: str = "application/json", - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, **extra: Any, ) -> Any: return params.Body( @@ -176,16 +176,16 @@ def Form( # noqa: N802 default: Any, *, media_type: str = "application/x-www-form-urlencoded", - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, **extra: Any, ) -> Any: return params.Form( @@ -209,16 +209,16 @@ def File( # noqa: N802 default: Any, *, media_type: str = "multipart/form-data", - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, **extra: Any, ) -> Any: return params.File( @@ -239,12 +239,15 @@ def File( # noqa: N802 def Depends( # noqa: N802 - dependency: Callable = None, *, use_cache: bool = True + dependency: Optional[Callable] = None, *, use_cache: bool = True ) -> Any: return params.Depends(dependency=dependency, use_cache=use_cache) def Security( # noqa: N802 - dependency: Callable = None, *, scopes: Sequence[str] = None, use_cache: bool = True + dependency: Optional[Callable] = None, + *, + scopes: Optional[Sequence[str]] = None, + use_cache: bool = True, ) -> Any: return params.Security(dependency=dependency, scopes=scopes, use_cache=use_cache) diff --git a/fastapi/params.py b/fastapi/params.py index c822cbfeb..aaa64ceba 100644 --- a/fastapi/params.py +++ b/fastapi/params.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Any, Callable, Sequence +from typing import Any, Callable, Optional, Sequence try: from pydantic.fields import FieldInfo @@ -22,17 +22,17 @@ class Param(FieldInfo): self, default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ): self.deprecated = deprecated @@ -62,17 +62,17 @@ class Path(Param): self, default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ): self.in_ = self.in_ @@ -100,17 +100,17 @@ class Query(Param): self, default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ): super().__init__( @@ -137,18 +137,18 @@ class Header(Param): self, default: Any, *, - alias: str = None, + alias: Optional[str] = None, convert_underscores: bool = True, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ): self.convert_underscores = convert_underscores @@ -176,17 +176,17 @@ class Cookie(Param): self, default: Any, *, - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, - deprecated: bool = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, + deprecated: Optional[bool] = None, **extra: Any, ): super().__init__( @@ -213,16 +213,16 @@ class Body(FieldInfo): *, embed: bool = False, media_type: str = "application/json", - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, **extra: Any, ): self.embed = embed @@ -252,16 +252,16 @@ class Form(Body): default: Any, *, media_type: str = "application/x-www-form-urlencoded", - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, **extra: Any, ): super().__init__( @@ -288,16 +288,16 @@ class File(Form): default: Any, *, media_type: str = "multipart/form-data", - alias: str = None, - title: str = None, - description: str = None, - gt: float = None, - ge: float = None, - lt: float = None, - le: float = None, - min_length: int = None, - max_length: int = None, - regex: str = None, + alias: Optional[str] = None, + title: Optional[str] = None, + description: Optional[str] = None, + gt: Optional[float] = None, + ge: Optional[float] = None, + lt: Optional[float] = None, + le: Optional[float] = None, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + regex: Optional[str] = None, **extra: Any, ): super().__init__( @@ -318,7 +318,9 @@ class File(Form): class Depends: - def __init__(self, dependency: Callable = None, *, use_cache: bool = True): + def __init__( + self, dependency: Optional[Callable] = None, *, use_cache: bool = True + ): self.dependency = dependency self.use_cache = use_cache @@ -331,9 +333,9 @@ class Depends: class Security(Depends): def __init__( self, - dependency: Callable = None, + dependency: Optional[Callable] = None, *, - scopes: Sequence[str] = None, + scopes: Optional[Sequence[str]] = None, use_cache: bool = True, ): super().__init__(dependency=dependency, use_cache=use_cache) diff --git a/fastapi/routing.py b/fastapi/routing.py index 9a4456550..5789d1c19 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -93,9 +93,9 @@ def _prepare_response_content( async def serialize_response( *, - field: ModelField = None, + field: Optional[ModelField] = None, response_content: Any, - include: Union[SetIntStr, DictIntStrAny] = None, + include: Optional[Union[SetIntStr, DictIntStrAny]] = None, exclude: Union[SetIntStr, DictIntStrAny] = set(), by_alias: bool = True, exclude_unset: bool = False, @@ -151,17 +151,17 @@ async def run_endpoint_function( def get_request_handler( dependant: Dependant, - body_field: ModelField = None, + body_field: Optional[ModelField] = None, status_code: int = 200, response_class: Type[Response] = JSONResponse, - response_field: ModelField = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + response_field: Optional[ModelField] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, - dependency_overrides_provider: Any = None, + dependency_overrides_provider: Optional[Any] = None, ) -> Callable: assert dependant.call is not None, "dependant.call must be a function" is_coroutine = asyncio.iscoroutinefunction(dependant.call) @@ -226,7 +226,7 @@ def get_request_handler( def get_websocket_app( - dependant: Dependant, dependency_overrides_provider: Any = None + dependant: Dependant, dependency_overrides_provider: Optional[Any] = None ) -> Callable: async def app(websocket: WebSocket) -> None: solved_result = await solve_dependencies( @@ -250,8 +250,8 @@ class APIWebSocketRoute(routing.WebSocketRoute): path: str, endpoint: Callable, *, - name: str = None, - dependency_overrides_provider: Any = None, + name: Optional[str] = None, + dependency_overrides_provider: Optional[Any] = None, ) -> None: self.path = path self.endpoint = endpoint @@ -272,19 +272,19 @@ class APIRoute(routing.Route): path: str, endpoint: Callable, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - name: str = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + name: Optional[str] = None, methods: Optional[Union[Set[str], List[str]]] = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, response_model_exclude_unset: bool = False, @@ -292,7 +292,7 @@ class APIRoute(routing.Route): response_model_exclude_none: bool = False, include_in_schema: bool = True, response_class: Optional[Type[Response]] = None, - dependency_overrides_provider: Any = None, + dependency_overrides_provider: Optional[Any] = None, callbacks: Optional[List["APIRoute"]] = None, ) -> None: # normalise enums e.g. http.HTTPStatus @@ -401,14 +401,14 @@ class APIRoute(routing.Route): class APIRouter(routing.Router): def __init__( self, - routes: List[routing.BaseRoute] = None, + routes: Optional[List[routing.BaseRoute]] = None, redirect_slashes: bool = True, - default: ASGIApp = None, - dependency_overrides_provider: Any = None, + default: Optional[ASGIApp] = None, + dependency_overrides_provider: Optional[Any] = None, route_class: Type[APIRoute] = APIRoute, - default_response_class: Type[Response] = None, - on_startup: Sequence[Callable] = None, - on_shutdown: Sequence[Callable] = None, + default_response_class: Optional[Type[Response]] = None, + on_startup: Optional[Sequence[Callable]] = None, + on_shutdown: Optional[Sequence[Callable]] = None, ) -> None: super().__init__( routes=routes, @@ -426,29 +426,29 @@ class APIRouter(routing.Router): path: str, endpoint: Callable, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, methods: Optional[Union[Set[str], List[str]]] = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, route_class_override: Optional[Type[APIRoute]] = None, - callbacks: List[APIRoute] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> None: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -487,28 +487,28 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - methods: List[str] = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + methods: Optional[List[str]] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -546,7 +546,7 @@ class APIRouter(routing.Router): return decorator def add_api_websocket_route( - self, path: str, endpoint: Callable, name: str = None + self, path: str, endpoint: Callable, name: Optional[str] = None ) -> None: route = APIWebSocketRoute( path, @@ -556,7 +556,7 @@ class APIRouter(routing.Router): ) self.routes.append(route) - def websocket(self, path: str, name: str = None) -> Callable: + def websocket(self, path: str, name: Optional[str] = None) -> Callable: def decorator(func: Callable) -> Callable: self.add_api_websocket_route(path, func, name=name) return func @@ -568,9 +568,9 @@ class APIRouter(routing.Router): router: "APIRouter", *, prefix: str = "", - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - responses: Dict[Union[int, str], Dict[str, Any]] = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, default_response_class: Optional[Type[Response]] = None, ) -> None: if prefix: @@ -643,27 +643,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -698,27 +698,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -753,27 +753,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -808,27 +808,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -863,27 +863,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -918,27 +918,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -973,27 +973,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover @@ -1028,27 +1028,27 @@ class APIRouter(routing.Router): self, path: str, *, - response_model: Type[Any] = None, + response_model: Optional[Type[Any]] = None, status_code: int = 200, - tags: List[str] = None, - dependencies: Sequence[params.Depends] = None, - summary: str = None, - description: str = None, + tags: Optional[List[str]] = None, + dependencies: Optional[Sequence[params.Depends]] = None, + summary: Optional[str] = None, + description: Optional[str] = None, response_description: str = "Successful Response", - responses: Dict[Union[int, str], Dict[str, Any]] = None, - deprecated: bool = None, - operation_id: str = None, - response_model_include: Union[SetIntStr, DictIntStrAny] = None, + responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None, + deprecated: Optional[bool] = None, + operation_id: Optional[str] = None, + response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None, response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_by_alias: bool = True, - response_model_skip_defaults: bool = None, + response_model_skip_defaults: Optional[bool] = None, response_model_exclude_unset: bool = False, response_model_exclude_defaults: bool = False, response_model_exclude_none: bool = False, include_in_schema: bool = True, - response_class: Type[Response] = None, - name: str = None, - callbacks: List[APIRoute] = None, + response_class: Optional[Type[Response]] = None, + name: Optional[str] = None, + callbacks: Optional[List[APIRoute]] = None, ) -> Callable: if response_model_skip_defaults is not None: warning_response_model_skip_defaults_deprecated() # pragma: nocover diff --git a/fastapi/security/api_key.py b/fastapi/security/api_key.py index 30fd3358f..e4dacb389 100644 --- a/fastapi/security/api_key.py +++ b/fastapi/security/api_key.py @@ -12,7 +12,9 @@ class APIKeyBase(SecurityBase): class APIKeyQuery(APIKeyBase): - def __init__(self, *, name: str, scheme_name: str = None, auto_error: bool = True): + def __init__( + self, *, name: str, scheme_name: Optional[str] = None, auto_error: bool = True + ): self.model: APIKey = APIKey(**{"in": APIKeyIn.query}, name=name) self.scheme_name = scheme_name or self.__class__.__name__ self.auto_error = auto_error @@ -30,7 +32,9 @@ class APIKeyQuery(APIKeyBase): class APIKeyHeader(APIKeyBase): - def __init__(self, *, name: str, scheme_name: str = None, auto_error: bool = True): + def __init__( + self, *, name: str, scheme_name: Optional[str] = None, auto_error: bool = True + ): self.model: APIKey = APIKey(**{"in": APIKeyIn.header}, name=name) self.scheme_name = scheme_name or self.__class__.__name__ self.auto_error = auto_error @@ -48,7 +52,9 @@ class APIKeyHeader(APIKeyBase): class APIKeyCookie(APIKeyBase): - def __init__(self, *, name: str, scheme_name: str = None, auto_error: bool = True): + def __init__( + self, *, name: str, scheme_name: Optional[str] = None, auto_error: bool = True + ): self.model: APIKey = APIKey(**{"in": APIKeyIn.cookie}, name=name) self.scheme_name = scheme_name or self.__class__.__name__ self.auto_error = auto_error diff --git a/fastapi/security/http.py b/fastapi/security/http.py index 3f2c1c390..80eec005d 100644 --- a/fastapi/security/http.py +++ b/fastapi/security/http.py @@ -24,7 +24,7 @@ class HTTPAuthorizationCredentials(BaseModel): class HTTPBase(SecurityBase): def __init__( - self, *, scheme: str, scheme_name: str = None, auto_error: bool = True + self, *, scheme: str, scheme_name: Optional[str] = None, auto_error: bool = True ): self.model = HTTPBaseModel(scheme=scheme) self.scheme_name = scheme_name or self.__class__.__name__ @@ -47,7 +47,11 @@ class HTTPBase(SecurityBase): class HTTPBasic(HTTPBase): def __init__( - self, *, scheme_name: str = None, realm: str = None, auto_error: bool = True + self, + *, + scheme_name: Optional[str] = None, + realm: Optional[str] = None, + auto_error: bool = True, ): self.model = HTTPBaseModel(scheme="basic") self.scheme_name = scheme_name or self.__class__.__name__ @@ -91,8 +95,8 @@ class HTTPBearer(HTTPBase): def __init__( self, *, - bearerFormat: str = None, - scheme_name: str = None, + bearerFormat: Optional[str] = None, + scheme_name: Optional[str] = None, auto_error: bool = True, ): self.model = HTTPBearerModel(bearerFormat=bearerFormat) @@ -123,7 +127,7 @@ class HTTPBearer(HTTPBase): class HTTPDigest(HTTPBase): - def __init__(self, *, scheme_name: str = None, auto_error: bool = True): + def __init__(self, *, scheme_name: Optional[str] = None, auto_error: bool = True): self.model = HTTPBaseModel(scheme="digest") self.scheme_name = scheme_name or self.__class__.__name__ self.auto_error = auto_error diff --git a/fastapi/security/oauth2.py b/fastapi/security/oauth2.py index 71bf02eed..84ddc4e5a 100644 --- a/fastapi/security/oauth2.py +++ b/fastapi/security/oauth2.py @@ -117,8 +117,8 @@ class OAuth2(SecurityBase): self, *, flows: OAuthFlowsModel = OAuthFlowsModel(), - scheme_name: str = None, - auto_error: bool = True + scheme_name: Optional[str] = None, + auto_error: Optional[bool] = True ): self.model = OAuth2Model(flows=flows) self.scheme_name = scheme_name or self.__class__.__name__ @@ -140,8 +140,8 @@ class OAuth2PasswordBearer(OAuth2): def __init__( self, tokenUrl: str, - scheme_name: str = None, - scopes: dict = None, + scheme_name: Optional[str] = None, + scopes: Optional[dict] = None, auto_error: bool = True, ): if not scopes: @@ -169,9 +169,9 @@ class OAuth2AuthorizationCodeBearer(OAuth2): self, authorizationUrl: str, tokenUrl: str, - refreshUrl: str = None, - scheme_name: str = None, - scopes: dict = None, + refreshUrl: Optional[str] = None, + scheme_name: Optional[str] = None, + scopes: Optional[dict] = None, auto_error: bool = True, ): if not scopes: @@ -202,6 +202,6 @@ class OAuth2AuthorizationCodeBearer(OAuth2): class SecurityScopes: - def __init__(self, scopes: List[str] = None): + def __init__(self, scopes: Optional[List[str]] = None): self.scopes = scopes or [] self.scope_str = " ".join(self.scopes) diff --git a/fastapi/security/open_id_connect_url.py b/fastapi/security/open_id_connect_url.py index f4d5ab3f4..a98c13f8a 100644 --- a/fastapi/security/open_id_connect_url.py +++ b/fastapi/security/open_id_connect_url.py @@ -9,7 +9,11 @@ from starlette.status import HTTP_403_FORBIDDEN class OpenIdConnect(SecurityBase): def __init__( - self, *, openIdConnectUrl: str, scheme_name: str = None, auto_error: bool = True + self, + *, + openIdConnectUrl: str, + scheme_name: Optional[str] = None, + auto_error: bool = True ): self.model = OpenIdConnectModel(openIdConnectUrl=openIdConnectUrl) self.scheme_name = scheme_name or self.__class__.__name__ diff --git a/fastapi/utils.py b/fastapi/utils.py index c388047cd..08b3e75f5 100644 --- a/fastapi/utils.py +++ b/fastapi/utils.py @@ -109,7 +109,9 @@ def create_response_field( def create_cloned_field( - field: ModelField, *, cloned_types: Dict[Type[BaseModel], Type[BaseModel]] = None, + field: ModelField, + *, + cloned_types: Optional[Dict[Type[BaseModel], Type[BaseModel]]] = None, ) -> ModelField: # _cloned_types has already cloned types, to support recursive models if cloned_types is None: diff --git a/tests/test_serialize_response.py b/tests/test_serialize_response.py index 31c08f50d..d823e5e04 100644 --- a/tests/test_serialize_response.py +++ b/tests/test_serialize_response.py @@ -10,7 +10,7 @@ app = FastAPI() class Item(BaseModel): name: str price: Optional[float] = None - owner_ids: List[int] = None + owner_ids: Optional[List[int]] = None @app.get("/items/valid", response_model=Item) diff --git a/tests/test_serialize_response_dataclass.py b/tests/test_serialize_response_dataclass.py index ea048127a..d1b64c4e8 100644 --- a/tests/test_serialize_response_dataclass.py +++ b/tests/test_serialize_response_dataclass.py @@ -11,7 +11,7 @@ app = FastAPI() class Item: name: str price: Optional[float] = None - owner_ids: List[int] = None + owner_ids: Optional[List[int]] = None @app.get("/items/valid", response_model=Item) diff --git a/tests/test_serialize_response_model.py b/tests/test_serialize_response_model.py index 056e9c4b0..295667437 100644 --- a/tests/test_serialize_response_model.py +++ b/tests/test_serialize_response_model.py @@ -10,7 +10,7 @@ app = FastAPI() class Item(BaseModel): name: str = Field(..., alias="aliased_name") price: Optional[float] = None - owner_ids: List[int] = None + owner_ids: Optional[List[int]] = None @app.get("/items/valid", response_model=Item) diff --git a/tests/test_validate_response.py b/tests/test_validate_response.py index e9a17e542..45d303e20 100644 --- a/tests/test_validate_response.py +++ b/tests/test_validate_response.py @@ -11,7 +11,7 @@ app = FastAPI() class Item(BaseModel): name: str price: Optional[float] = None - owner_ids: List[int] = None + owner_ids: Optional[List[int]] = None @app.get("/items/invalid", response_model=Item) diff --git a/tests/test_validate_response_dataclass.py b/tests/test_validate_response_dataclass.py index 1765dcb43..f2cfa7a11 100644 --- a/tests/test_validate_response_dataclass.py +++ b/tests/test_validate_response_dataclass.py @@ -13,7 +13,7 @@ app = FastAPI() class Item: name: str price: Optional[float] = None - owner_ids: List[int] = None + owner_ids: Optional[List[int]] = None @app.get("/items/invalid", response_model=Item)