Skip to content

Commit

Permalink
Cutefy Adrian's improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Mar 5, 2023
1 parent c791251 commit b5aac4d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions starlette/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ async def no_state_wrapper(scope, receive, send):
await app(scope, receive, send)

with pytest.raises(
RuntimeError, match='This server does not support "state" in the lifespan scope'
RuntimeError, match='The server does not support "state" in the lifespan scope'
):
with test_client_factory(no_state_wrapper):
raise AssertionError("Should not be called") # pragma: no cover
Expand Down

0 comments on commit b5aac4d

Please sign in to comment.