Browse Source

🎨 Add typing.Optional to variables that accept None as value (#1731)

Co-authored-by: nimashadix <[email protected]>
Co-authored-by: Sebastián Ramírez <[email protected]>
pull/1753/head
Nima Mashhadi M. Reza 5 years ago
committed by GitHub
parent
commit
3223de5598
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 282
      fastapi/applications.py
  2. 38
      fastapi/dependencies/models.py
  3. 27
      fastapi/dependencies/utils.py
  4. 6
      fastapi/encoders.py
  5. 7
      fastapi/exceptions.py
  6. 2
      fastapi/openapi/utils.py
  7. 157
      fastapi/param_functions.py
  8. 180
      fastapi/params.py
  9. 324
      fastapi/routing.py
  10. 12
      fastapi/security/api_key.py
  11. 14
      fastapi/security/http.py
  12. 16
      fastapi/security/oauth2.py
  13. 6
      fastapi/security/open_id_connect_url.py
  14. 4
      fastapi/utils.py
  15. 2
      tests/test_serialize_response.py
  16. 2
      tests/test_serialize_response_dataclass.py
  17. 2
      tests/test_serialize_response_model.py
  18. 2
      tests/test_validate_response.py
  19. 2
      tests/test_validate_response_dataclass.py

282
fastapi/applications.py

@ -32,7 +32,7 @@ class FastAPI(Starlette):
self, self,
*, *,
debug: bool = False, debug: bool = False,
routes: List[BaseRoute] = None, routes: Optional[List[BaseRoute]] = None,
title: str = "FastAPI", title: str = "FastAPI",
description: str = "", description: str = "",
version: str = "0.1.0", version: str = "0.1.0",
@ -44,10 +44,12 @@ class FastAPI(Starlette):
redoc_url: Optional[str] = "/redoc", redoc_url: Optional[str] = "/redoc",
swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect", swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
swagger_ui_init_oauth: Optional[dict] = None, swagger_ui_init_oauth: Optional[dict] = None,
middleware: Sequence[Middleware] = None, middleware: Optional[Sequence[Middleware]] = None,
exception_handlers: Dict[Union[int, Type[Exception]], Callable] = None, exception_handlers: Optional[
on_startup: Sequence[Callable] = None, Dict[Union[int, Type[Exception]], Callable]
on_shutdown: Sequence[Callable] = None, ] = None,
on_startup: Optional[Sequence[Callable]] = None,
on_shutdown: Optional[Sequence[Callable]] = None,
openapi_prefix: str = "", openapi_prefix: str = "",
root_path: str = "", root_path: str = "",
root_path_in_servers: bool = True, root_path_in_servers: bool = True,
@ -187,27 +189,27 @@ class FastAPI(Starlette):
path: str, path: str,
endpoint: Callable, endpoint: Callable,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
methods: List[str] = None, methods: Optional[List[str]] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
) -> None: ) -> None:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -242,27 +244,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
methods: List[str] = None, methods: Optional[List[str]] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -299,11 +301,11 @@ class FastAPI(Starlette):
return decorator return decorator
def add_api_websocket_route( def add_api_websocket_route(
self, path: str, endpoint: Callable, name: str = None self, path: str, endpoint: Callable, name: Optional[str] = None
) -> None: ) -> None:
self.router.add_api_websocket_route(path, endpoint, name=name) 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: def decorator(func: Callable) -> Callable:
self.add_api_websocket_route(path, func, name=name) self.add_api_websocket_route(path, func, name=name)
return func return func
@ -315,9 +317,9 @@ class FastAPI(Starlette):
router: routing.APIRouter, router: routing.APIRouter,
*, *,
prefix: str = "", prefix: str = "",
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
default_response_class: Optional[Type[Response]] = None, default_response_class: Optional[Type[Response]] = None,
) -> None: ) -> None:
self.router.include_router( self.router.include_router(
@ -334,27 +336,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -388,27 +390,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -442,27 +444,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -496,27 +498,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -550,27 +552,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -604,27 +606,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -658,27 +660,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -712,27 +714,27 @@ class FastAPI(Starlette):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[Depends] = None, dependencies: Optional[Sequence[Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[routing.APIRoute] = None, callbacks: Optional[List[routing.APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover

38
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 from fastapi.security.base import SecurityBase
@ -12,7 +12,9 @@ param_supported_types = (str, int, float, bool)
class SecurityRequirement: 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.security_scheme = security_scheme
self.scopes = scopes self.scopes = scopes
@ -21,23 +23,23 @@ class Dependant:
def __init__( def __init__(
self, self,
*, *,
path_params: List[ModelField] = None, path_params: Optional[List[ModelField]] = None,
query_params: List[ModelField] = None, query_params: Optional[List[ModelField]] = None,
header_params: List[ModelField] = None, header_params: Optional[List[ModelField]] = None,
cookie_params: List[ModelField] = None, cookie_params: Optional[List[ModelField]] = None,
body_params: List[ModelField] = None, body_params: Optional[List[ModelField]] = None,
dependencies: List["Dependant"] = None, dependencies: Optional[List["Dependant"]] = None,
security_schemes: List[SecurityRequirement] = None, security_schemes: Optional[List[SecurityRequirement]] = None,
name: str = None, name: Optional[str] = None,
call: Callable = None, call: Optional[Callable] = None,
request_param_name: str = None, request_param_name: Optional[str] = None,
websocket_param_name: str = None, websocket_param_name: Optional[str] = None,
response_param_name: str = None, response_param_name: Optional[str] = None,
background_tasks_param_name: str = None, background_tasks_param_name: Optional[str] = None,
security_scopes_param_name: str = None, security_scopes_param_name: Optional[str] = None,
security_scopes: List[str] = None, security_scopes: Optional[List[str]] = None,
use_cache: bool = True, use_cache: bool = True,
path: str = None, path: Optional[str] = None,
) -> None: ) -> None:
self.path_params = path_params or [] self.path_params = path_params or []
self.query_params = query_params or [] self.query_params = query_params or []

27
fastapi/dependencies/utils.py

@ -97,7 +97,7 @@ sequence_shape_to_type = {
def get_param_sub_dependant( 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: ) -> Dependant:
depends: params.Depends = param.default depends: params.Depends = param.default
if depends.dependency: if depends.dependency:
@ -125,8 +125,8 @@ def get_sub_dependant(
depends: params.Depends, depends: params.Depends,
dependency: Callable, dependency: Callable,
path: str, path: str,
name: str = None, name: Optional[str] = None,
security_scopes: List[str] = None, security_scopes: Optional[List[str]] = None,
) -> Dependant: ) -> Dependant:
security_requirement = None security_requirement = None
security_scopes = security_scopes or [] security_scopes = security_scopes or []
@ -157,7 +157,10 @@ CacheKey = Tuple[Optional[Callable], Tuple[str, ...]]
def get_flat_dependant( 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: ) -> Dependant:
if visited is None: if visited is None:
visited = [] visited = []
@ -269,8 +272,8 @@ def get_dependant(
*, *,
path: str, path: str,
call: Callable, call: Callable,
name: str = None, name: Optional[str] = None,
security_scopes: List[str] = None, security_scopes: Optional[List[str]] = None,
use_cache: bool = True, use_cache: bool = True,
) -> Dependant: ) -> Dependant:
path_param_names = get_path_param_names(path) path_param_names = get_path_param_names(path)
@ -348,7 +351,7 @@ def get_param_field(
param: inspect.Parameter, param: inspect.Parameter,
param_name: str, param_name: str,
default_field_info: Type[params.Param] = params.Param, default_field_info: Type[params.Param] = params.Param,
force_type: params.ParamTypes = None, force_type: Optional[params.ParamTypes] = None,
ignore_default: bool = False, ignore_default: bool = False,
) -> ModelField: ) -> ModelField:
default_value = Required default_value = Required
@ -456,10 +459,10 @@ async def solve_dependencies(
request: Union[Request, WebSocket], request: Union[Request, WebSocket],
dependant: Dependant, dependant: Dependant,
body: Optional[Union[Dict[str, Any], FormData]] = None, body: Optional[Union[Dict[str, Any], FormData]] = None,
background_tasks: BackgroundTasks = None, background_tasks: Optional[BackgroundTasks] = None,
response: Response = None, response: Optional[Response] = None,
dependency_overrides_provider: Any = None, dependency_overrides_provider: Optional[Any] = None,
dependency_cache: Dict[Tuple[Callable, Tuple[str]], Any] = None, dependency_cache: Optional[Dict[Tuple[Callable, Tuple[str]], Any]] = None,
) -> Tuple[ ) -> Tuple[
Dict[str, Any], Dict[str, Any],
List[ErrorWrapper], List[ErrorWrapper],
@ -653,7 +656,7 @@ async def request_body_to_args(
else: else:
loc = ("body", field.alias) loc = ("body", field.alias)
value: Any = None value: Optional[Any] = None
if received_body is not None: if received_body is not None:
if ( if (
field.shape in sequence_shapes or field.type_ in sequence_types field.shape in sequence_shapes or field.type_ in sequence_types

6
fastapi/encoders.py

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
from pathlib import PurePath from pathlib import PurePath
from types import GeneratorType 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.logger import logger
from fastapi.utils import PYDANTIC_1 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( def jsonable_encoder(
obj: Any, obj: Any,
include: Union[SetIntStr, DictIntStrAny] = None, include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
exclude: Union[SetIntStr, DictIntStrAny] = set(), exclude: Union[SetIntStr, DictIntStrAny] = set(),
by_alias: bool = True, by_alias: bool = True,
skip_defaults: bool = None, skip_defaults: Optional[bool] = None,
exclude_unset: bool = False, exclude_unset: bool = False,
exclude_defaults: bool = False, exclude_defaults: bool = False,
exclude_none: bool = False, exclude_none: bool = False,

7
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 fastapi.utils import PYDANTIC_1
from pydantic import ValidationError, create_model from pydantic import ValidationError, create_model
@ -10,7 +10,10 @@ from starlette.websockets import WebSocket
class HTTPException(StarletteHTTPException): class HTTPException(StarletteHTTPException):
def __init__( 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: ) -> None:
super().__init__(status_code=status_code, detail=detail) super().__init__(status_code=status_code, detail=detail)
self.headers = headers self.headers = headers

2
fastapi/openapi/utils.py

@ -329,7 +329,7 @@ def get_openapi(
title: str, title: str,
version: str, version: str,
openapi_version: str = "3.0.2", openapi_version: str = "3.0.2",
description: str = None, description: Optional[str] = None,
routes: Sequence[BaseRoute], routes: Sequence[BaseRoute],
tags: Optional[List[Dict[str, Any]]] = None, tags: Optional[List[Dict[str, Any]]] = None,
servers: Optional[List[Dict[str, Union[str, Any]]]] = None, servers: Optional[List[Dict[str, Union[str, Any]]]] = None,

157
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 from fastapi import params
@ -6,17 +6,17 @@ from fastapi import params
def Path( # noqa: N802 def Path( # noqa: N802
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.Path( return params.Path(
@ -39,17 +39,17 @@ def Path( # noqa: N802
def Query( # noqa: N802 def Query( # noqa: N802
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.Query( return params.Query(
@ -72,18 +72,18 @@ def Query( # noqa: N802
def Header( # noqa: N802 def Header( # noqa: N802
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
convert_underscores: bool = True, convert_underscores: bool = True,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.Header( return params.Header(
@ -107,17 +107,17 @@ def Header( # noqa: N802
def Cookie( # noqa: N802 def Cookie( # noqa: N802
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.Cookie( return params.Cookie(
@ -142,16 +142,16 @@ def Body( # noqa: N802
*, *,
embed: bool = False, embed: bool = False,
media_type: str = "application/json", media_type: str = "application/json",
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.Body( return params.Body(
@ -176,16 +176,16 @@ def Form( # noqa: N802
default: Any, default: Any,
*, *,
media_type: str = "application/x-www-form-urlencoded", media_type: str = "application/x-www-form-urlencoded",
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.Form( return params.Form(
@ -209,16 +209,16 @@ def File( # noqa: N802
default: Any, default: Any,
*, *,
media_type: str = "multipart/form-data", media_type: str = "multipart/form-data",
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
**extra: Any, **extra: Any,
) -> Any: ) -> Any:
return params.File( return params.File(
@ -239,12 +239,15 @@ def File( # noqa: N802
def Depends( # noqa: N802 def Depends( # noqa: N802
dependency: Callable = None, *, use_cache: bool = True dependency: Optional[Callable] = None, *, use_cache: bool = True
) -> Any: ) -> Any:
return params.Depends(dependency=dependency, use_cache=use_cache) return params.Depends(dependency=dependency, use_cache=use_cache)
def Security( # noqa: N802 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: ) -> Any:
return params.Security(dependency=dependency, scopes=scopes, use_cache=use_cache) return params.Security(dependency=dependency, scopes=scopes, use_cache=use_cache)

180
fastapi/params.py

@ -1,5 +1,5 @@
from enum import Enum from enum import Enum
from typing import Any, Callable, Sequence from typing import Any, Callable, Optional, Sequence
try: try:
from pydantic.fields import FieldInfo from pydantic.fields import FieldInfo
@ -22,17 +22,17 @@ class Param(FieldInfo):
self, self,
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
): ):
self.deprecated = deprecated self.deprecated = deprecated
@ -62,17 +62,17 @@ class Path(Param):
self, self,
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
): ):
self.in_ = self.in_ self.in_ = self.in_
@ -100,17 +100,17 @@ class Query(Param):
self, self,
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
): ):
super().__init__( super().__init__(
@ -137,18 +137,18 @@ class Header(Param):
self, self,
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
convert_underscores: bool = True, convert_underscores: bool = True,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
): ):
self.convert_underscores = convert_underscores self.convert_underscores = convert_underscores
@ -176,17 +176,17 @@ class Cookie(Param):
self, self,
default: Any, default: Any,
*, *,
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
**extra: Any, **extra: Any,
): ):
super().__init__( super().__init__(
@ -213,16 +213,16 @@ class Body(FieldInfo):
*, *,
embed: bool = False, embed: bool = False,
media_type: str = "application/json", media_type: str = "application/json",
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
**extra: Any, **extra: Any,
): ):
self.embed = embed self.embed = embed
@ -252,16 +252,16 @@ class Form(Body):
default: Any, default: Any,
*, *,
media_type: str = "application/x-www-form-urlencoded", media_type: str = "application/x-www-form-urlencoded",
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
**extra: Any, **extra: Any,
): ):
super().__init__( super().__init__(
@ -288,16 +288,16 @@ class File(Form):
default: Any, default: Any,
*, *,
media_type: str = "multipart/form-data", media_type: str = "multipart/form-data",
alias: str = None, alias: Optional[str] = None,
title: str = None, title: Optional[str] = None,
description: str = None, description: Optional[str] = None,
gt: float = None, gt: Optional[float] = None,
ge: float = None, ge: Optional[float] = None,
lt: float = None, lt: Optional[float] = None,
le: float = None, le: Optional[float] = None,
min_length: int = None, min_length: Optional[int] = None,
max_length: int = None, max_length: Optional[int] = None,
regex: str = None, regex: Optional[str] = None,
**extra: Any, **extra: Any,
): ):
super().__init__( super().__init__(
@ -318,7 +318,9 @@ class File(Form):
class Depends: 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.dependency = dependency
self.use_cache = use_cache self.use_cache = use_cache
@ -331,9 +333,9 @@ class Depends:
class Security(Depends): class Security(Depends):
def __init__( def __init__(
self, self,
dependency: Callable = None, dependency: Optional[Callable] = None,
*, *,
scopes: Sequence[str] = None, scopes: Optional[Sequence[str]] = None,
use_cache: bool = True, use_cache: bool = True,
): ):
super().__init__(dependency=dependency, use_cache=use_cache) super().__init__(dependency=dependency, use_cache=use_cache)

324
fastapi/routing.py

@ -93,9 +93,9 @@ def _prepare_response_content(
async def serialize_response( async def serialize_response(
*, *,
field: ModelField = None, field: Optional[ModelField] = None,
response_content: Any, response_content: Any,
include: Union[SetIntStr, DictIntStrAny] = None, include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
exclude: Union[SetIntStr, DictIntStrAny] = set(), exclude: Union[SetIntStr, DictIntStrAny] = set(),
by_alias: bool = True, by_alias: bool = True,
exclude_unset: bool = False, exclude_unset: bool = False,
@ -151,17 +151,17 @@ async def run_endpoint_function(
def get_request_handler( def get_request_handler(
dependant: Dependant, dependant: Dependant,
body_field: ModelField = None, body_field: Optional[ModelField] = None,
status_code: int = 200, status_code: int = 200,
response_class: Type[Response] = JSONResponse, response_class: Type[Response] = JSONResponse,
response_field: ModelField = None, response_field: Optional[ModelField] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False, response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
dependency_overrides_provider: Any = None, dependency_overrides_provider: Optional[Any] = None,
) -> Callable: ) -> Callable:
assert dependant.call is not None, "dependant.call must be a function" assert dependant.call is not None, "dependant.call must be a function"
is_coroutine = asyncio.iscoroutinefunction(dependant.call) is_coroutine = asyncio.iscoroutinefunction(dependant.call)
@ -226,7 +226,7 @@ def get_request_handler(
def get_websocket_app( def get_websocket_app(
dependant: Dependant, dependency_overrides_provider: Any = None dependant: Dependant, dependency_overrides_provider: Optional[Any] = None
) -> Callable: ) -> Callable:
async def app(websocket: WebSocket) -> None: async def app(websocket: WebSocket) -> None:
solved_result = await solve_dependencies( solved_result = await solve_dependencies(
@ -250,8 +250,8 @@ class APIWebSocketRoute(routing.WebSocketRoute):
path: str, path: str,
endpoint: Callable, endpoint: Callable,
*, *,
name: str = None, name: Optional[str] = None,
dependency_overrides_provider: Any = None, dependency_overrides_provider: Optional[Any] = None,
) -> None: ) -> None:
self.path = path self.path = path
self.endpoint = endpoint self.endpoint = endpoint
@ -272,19 +272,19 @@ class APIRoute(routing.Route):
path: str, path: str,
endpoint: Callable, endpoint: Callable,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
name: str = None, name: Optional[str] = None,
methods: Optional[Union[Set[str], List[str]]] = None, methods: Optional[Union[Set[str], List[str]]] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False, response_model_exclude_unset: bool = False,
@ -292,7 +292,7 @@ class APIRoute(routing.Route):
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Optional[Type[Response]] = None, response_class: Optional[Type[Response]] = None,
dependency_overrides_provider: Any = None, dependency_overrides_provider: Optional[Any] = None,
callbacks: Optional[List["APIRoute"]] = None, callbacks: Optional[List["APIRoute"]] = None,
) -> None: ) -> None:
# normalise enums e.g. http.HTTPStatus # normalise enums e.g. http.HTTPStatus
@ -401,14 +401,14 @@ class APIRoute(routing.Route):
class APIRouter(routing.Router): class APIRouter(routing.Router):
def __init__( def __init__(
self, self,
routes: List[routing.BaseRoute] = None, routes: Optional[List[routing.BaseRoute]] = None,
redirect_slashes: bool = True, redirect_slashes: bool = True,
default: ASGIApp = None, default: Optional[ASGIApp] = None,
dependency_overrides_provider: Any = None, dependency_overrides_provider: Optional[Any] = None,
route_class: Type[APIRoute] = APIRoute, route_class: Type[APIRoute] = APIRoute,
default_response_class: Type[Response] = None, default_response_class: Optional[Type[Response]] = None,
on_startup: Sequence[Callable] = None, on_startup: Optional[Sequence[Callable]] = None,
on_shutdown: Sequence[Callable] = None, on_shutdown: Optional[Sequence[Callable]] = None,
) -> None: ) -> None:
super().__init__( super().__init__(
routes=routes, routes=routes,
@ -426,29 +426,29 @@ class APIRouter(routing.Router):
path: str, path: str,
endpoint: Callable, endpoint: Callable,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
methods: Optional[Union[Set[str], List[str]]] = None, methods: Optional[Union[Set[str], List[str]]] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
route_class_override: Optional[Type[APIRoute]] = None, route_class_override: Optional[Type[APIRoute]] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> None: ) -> None:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -487,28 +487,28 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
methods: List[str] = None, methods: Optional[List[str]] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -546,7 +546,7 @@ class APIRouter(routing.Router):
return decorator return decorator
def add_api_websocket_route( def add_api_websocket_route(
self, path: str, endpoint: Callable, name: str = None self, path: str, endpoint: Callable, name: Optional[str] = None
) -> None: ) -> None:
route = APIWebSocketRoute( route = APIWebSocketRoute(
path, path,
@ -556,7 +556,7 @@ class APIRouter(routing.Router):
) )
self.routes.append(route) 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: def decorator(func: Callable) -> Callable:
self.add_api_websocket_route(path, func, name=name) self.add_api_websocket_route(path, func, name=name)
return func return func
@ -568,9 +568,9 @@ class APIRouter(routing.Router):
router: "APIRouter", router: "APIRouter",
*, *,
prefix: str = "", prefix: str = "",
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
default_response_class: Optional[Type[Response]] = None, default_response_class: Optional[Type[Response]] = None,
) -> None: ) -> None:
if prefix: if prefix:
@ -643,27 +643,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -698,27 +698,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -753,27 +753,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -808,27 +808,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -863,27 +863,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -918,27 +918,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -973,27 +973,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover
@ -1028,27 +1028,27 @@ class APIRouter(routing.Router):
self, self,
path: str, path: str,
*, *,
response_model: Type[Any] = None, response_model: Optional[Type[Any]] = None,
status_code: int = 200, status_code: int = 200,
tags: List[str] = None, tags: Optional[List[str]] = None,
dependencies: Sequence[params.Depends] = None, dependencies: Optional[Sequence[params.Depends]] = None,
summary: str = None, summary: Optional[str] = None,
description: str = None, description: Optional[str] = None,
response_description: str = "Successful Response", response_description: str = "Successful Response",
responses: Dict[Union[int, str], Dict[str, Any]] = None, responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
deprecated: bool = None, deprecated: Optional[bool] = None,
operation_id: str = None, operation_id: Optional[str] = None,
response_model_include: Union[SetIntStr, DictIntStrAny] = None, response_model_include: Optional[Union[SetIntStr, DictIntStrAny]] = None,
response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(), response_model_exclude: Union[SetIntStr, DictIntStrAny] = set(),
response_model_by_alias: bool = True, 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_unset: bool = False,
response_model_exclude_defaults: bool = False, response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False, response_model_exclude_none: bool = False,
include_in_schema: bool = True, include_in_schema: bool = True,
response_class: Type[Response] = None, response_class: Optional[Type[Response]] = None,
name: str = None, name: Optional[str] = None,
callbacks: List[APIRoute] = None, callbacks: Optional[List[APIRoute]] = None,
) -> Callable: ) -> Callable:
if response_model_skip_defaults is not None: if response_model_skip_defaults is not None:
warning_response_model_skip_defaults_deprecated() # pragma: nocover warning_response_model_skip_defaults_deprecated() # pragma: nocover

12
fastapi/security/api_key.py

@ -12,7 +12,9 @@ class APIKeyBase(SecurityBase):
class APIKeyQuery(APIKeyBase): 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.model: APIKey = APIKey(**{"in": APIKeyIn.query}, name=name)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error = auto_error
@ -30,7 +32,9 @@ class APIKeyQuery(APIKeyBase):
class APIKeyHeader(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.model: APIKey = APIKey(**{"in": APIKeyIn.header}, name=name)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error = auto_error
@ -48,7 +52,9 @@ class APIKeyHeader(APIKeyBase):
class APIKeyCookie(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.model: APIKey = APIKey(**{"in": APIKeyIn.cookie}, name=name)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error = auto_error

14
fastapi/security/http.py

@ -24,7 +24,7 @@ class HTTPAuthorizationCredentials(BaseModel):
class HTTPBase(SecurityBase): class HTTPBase(SecurityBase):
def __init__( 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.model = HTTPBaseModel(scheme=scheme)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
@ -47,7 +47,11 @@ class HTTPBase(SecurityBase):
class HTTPBasic(HTTPBase): class HTTPBasic(HTTPBase):
def __init__( 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.model = HTTPBaseModel(scheme="basic")
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
@ -91,8 +95,8 @@ class HTTPBearer(HTTPBase):
def __init__( def __init__(
self, self,
*, *,
bearerFormat: str = None, bearerFormat: Optional[str] = None,
scheme_name: str = None, scheme_name: Optional[str] = None,
auto_error: bool = True, auto_error: bool = True,
): ):
self.model = HTTPBearerModel(bearerFormat=bearerFormat) self.model = HTTPBearerModel(bearerFormat=bearerFormat)
@ -123,7 +127,7 @@ class HTTPBearer(HTTPBase):
class HTTPDigest(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.model = HTTPBaseModel(scheme="digest")
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error = auto_error

16
fastapi/security/oauth2.py

@ -117,8 +117,8 @@ class OAuth2(SecurityBase):
self, self,
*, *,
flows: OAuthFlowsModel = OAuthFlowsModel(), flows: OAuthFlowsModel = OAuthFlowsModel(),
scheme_name: str = None, scheme_name: Optional[str] = None,
auto_error: bool = True auto_error: Optional[bool] = True
): ):
self.model = OAuth2Model(flows=flows) self.model = OAuth2Model(flows=flows)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
@ -140,8 +140,8 @@ class OAuth2PasswordBearer(OAuth2):
def __init__( def __init__(
self, self,
tokenUrl: str, tokenUrl: str,
scheme_name: str = None, scheme_name: Optional[str] = None,
scopes: dict = None, scopes: Optional[dict] = None,
auto_error: bool = True, auto_error: bool = True,
): ):
if not scopes: if not scopes:
@ -169,9 +169,9 @@ class OAuth2AuthorizationCodeBearer(OAuth2):
self, self,
authorizationUrl: str, authorizationUrl: str,
tokenUrl: str, tokenUrl: str,
refreshUrl: str = None, refreshUrl: Optional[str] = None,
scheme_name: str = None, scheme_name: Optional[str] = None,
scopes: dict = None, scopes: Optional[dict] = None,
auto_error: bool = True, auto_error: bool = True,
): ):
if not scopes: if not scopes:
@ -202,6 +202,6 @@ class OAuth2AuthorizationCodeBearer(OAuth2):
class SecurityScopes: class SecurityScopes:
def __init__(self, scopes: List[str] = None): def __init__(self, scopes: Optional[List[str]] = None):
self.scopes = scopes or [] self.scopes = scopes or []
self.scope_str = " ".join(self.scopes) self.scope_str = " ".join(self.scopes)

6
fastapi/security/open_id_connect_url.py

@ -9,7 +9,11 @@ from starlette.status import HTTP_403_FORBIDDEN
class OpenIdConnect(SecurityBase): class OpenIdConnect(SecurityBase):
def __init__( 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.model = OpenIdConnectModel(openIdConnectUrl=openIdConnectUrl)
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__

4
fastapi/utils.py

@ -109,7 +109,9 @@ def create_response_field(
def create_cloned_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: ) -> ModelField:
# _cloned_types has already cloned types, to support recursive models # _cloned_types has already cloned types, to support recursive models
if cloned_types is None: if cloned_types is None:

2
tests/test_serialize_response.py

@ -10,7 +10,7 @@ app = FastAPI()
class Item(BaseModel): class Item(BaseModel):
name: str name: str
price: Optional[float] = None price: Optional[float] = None
owner_ids: List[int] = None owner_ids: Optional[List[int]] = None
@app.get("/items/valid", response_model=Item) @app.get("/items/valid", response_model=Item)

2
tests/test_serialize_response_dataclass.py

@ -11,7 +11,7 @@ app = FastAPI()
class Item: class Item:
name: str name: str
price: Optional[float] = None price: Optional[float] = None
owner_ids: List[int] = None owner_ids: Optional[List[int]] = None
@app.get("/items/valid", response_model=Item) @app.get("/items/valid", response_model=Item)

2
tests/test_serialize_response_model.py

@ -10,7 +10,7 @@ app = FastAPI()
class Item(BaseModel): class Item(BaseModel):
name: str = Field(..., alias="aliased_name") name: str = Field(..., alias="aliased_name")
price: Optional[float] = None price: Optional[float] = None
owner_ids: List[int] = None owner_ids: Optional[List[int]] = None
@app.get("/items/valid", response_model=Item) @app.get("/items/valid", response_model=Item)

2
tests/test_validate_response.py

@ -11,7 +11,7 @@ app = FastAPI()
class Item(BaseModel): class Item(BaseModel):
name: str name: str
price: Optional[float] = None price: Optional[float] = None
owner_ids: List[int] = None owner_ids: Optional[List[int]] = None
@app.get("/items/invalid", response_model=Item) @app.get("/items/invalid", response_model=Item)

2
tests/test_validate_response_dataclass.py

@ -13,7 +13,7 @@ app = FastAPI()
class Item: class Item:
name: str name: str
price: Optional[float] = None price: Optional[float] = None
owner_ids: List[int] = None owner_ids: Optional[List[int]] = None
@app.get("/items/invalid", response_model=Item) @app.get("/items/invalid", response_model=Item)

Loading…
Cancel
Save