Browse Source

Declare state property on a class level

pull/14159/head
Diego Romero 10 months ago
parent
commit
2312b6038a
  1. 42
      fastapi/applications.py

42
fastapi/applications.py

@ -64,6 +64,24 @@ class FastAPI(Starlette):
``` ```
""" """
state: Annotated[
State,
Doc(
"""
A state object for the application. This is the same object for the
entire application, it doesn't change from request to request.
You normally wouldn't use this in FastAPI, for most of the cases you
would instead use FastAPI dependencies.
This is simply inherited from Starlette.
Read more about it in the
[Starlette docs for Applications](https://www.starlette.io/applications/#storing-state-on-the-app-instance).
"""
),
]
def __init__( def __init__(
self: AppType, self: AppType,
*, *,
@ -923,25 +941,11 @@ class FastAPI(Starlette):
[FastAPI docs for OpenAPI Webhooks](https://fastapi.tiangolo.com/advanced/openapi-webhooks/). [FastAPI docs for OpenAPI Webhooks](https://fastapi.tiangolo.com/advanced/openapi-webhooks/).
""" """
), ),
] = webhooks or routing.APIRouter() ] = (
webhooks or routing.APIRouter()
)
self.root_path = root_path or openapi_prefix self.root_path = root_path or openapi_prefix
self.state: Annotated[ self.state = State()
State,
Doc(
"""
A state object for the application. This is the same object for the
entire application, it doesn't change from request to request.
You normally wouldn't use this in FastAPI, for most of the cases you
would instead use FastAPI dependencies.
This is simply inherited from Starlette.
Read more about it in the
[Starlette docs for Applications](https://www.starlette.io/applications/#storing-state-on-the-app-instance).
"""
),
] = State()
self.dependency_overrides: Annotated[ self.dependency_overrides: Annotated[
Dict[Callable[..., Any], Callable[..., Any]], Dict[Callable[..., Any], Callable[..., Any]],
Doc( Doc(
@ -976,7 +980,7 @@ class FastAPI(Starlette):
) )
self.exception_handlers: Dict[ self.exception_handlers: Dict[
Any, Callable[[Request, Any], Union[Response, Awaitable[Response]]] Any, Callable[[Request, Any], Union[Response, Awaitable[Response]]]
] = {} if exception_handlers is None else dict(exception_handlers) ] = ({} if exception_handlers is None else dict(exception_handlers))
self.exception_handlers.setdefault(HTTPException, http_exception_handler) self.exception_handlers.setdefault(HTTPException, http_exception_handler)
self.exception_handlers.setdefault( self.exception_handlers.setdefault(
RequestValidationError, request_validation_exception_handler RequestValidationError, request_validation_exception_handler

Loading…
Cancel
Save