From 2312b6038ac7c524fd20a7345448570b63e85fc2 Mon Sep 17 00:00:00 2001 From: Diego Romero Date: Sun, 5 Oct 2025 18:46:51 -0300 Subject: [PATCH] Declare state property on a class level --- fastapi/applications.py | 42 ++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 915f5f70a..b8f2096c7 100644 --- a/fastapi/applications.py +++ b/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__( self: AppType, *, @@ -923,25 +941,11 @@ class FastAPI(Starlette): [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.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). - """ - ), - ] = State() + self.state = State() self.dependency_overrides: Annotated[ Dict[Callable[..., Any], Callable[..., Any]], Doc( @@ -976,7 +980,7 @@ class FastAPI(Starlette): ) self.exception_handlers: Dict[ 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( RequestValidationError, request_validation_exception_handler