From f311c961ef2231eeefdaf33798335efdcf36b82f Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 5 Mar 2023 14:41:30 +0100 Subject: [PATCH] Cutefy Adrian's improvements --- starlette/routing.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index e8fc47074f..92b81f0bc2 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -679,22 +679,20 @@ async def lifespan(self, scope: Scope, receive: Receive, send: Send) -> None: """ started = False app = scope.get("app") + state = scope.get("state") await receive() lifespan_needs_state = ( len(inspect.signature(self.lifespan_context).parameters) == 2 ) - server_supports_state = "state" in scope + server_supports_state = state is not None if lifespan_needs_state and not server_supports_state: raise RuntimeError( - 'This server does not support "state" in the lifespan scope.' - " Please try updating your ASGI server." + 'The server does not support "state" in the lifespan scope.' ) try: lifespan_context: Lifespan if lifespan_needs_state: - lifespan_context = functools.partial( - self.lifespan_context, state=scope["state"] - ) + lifespan_context = functools.partial(self.lifespan_context, state=state) else: lifespan_context = typing.cast(StatelessLifespan, self.lifespan_context) async with lifespan_context(app):