Skip to content

Pass inner app results through the middleware#2

Merged
pydanny merged 2 commits intomainfrom
fix-middleware-return-await
Mar 13, 2026
Merged

Pass inner app results through the middleware#2
pydanny merged 2 commits intomainfrom
fix-middleware-return-await

Conversation

@audreyfeldroy
Copy link
Copy Markdown
Member

Summary

StaticRewriteMiddleware.call used bare await self.app() on both
code paths, which silently discards whatever the inner app returns.
Correct ASGI middleware uses return await self.app() so the result
propagates through the chain. This matters for both wiring patterns:

  • app = StaticRewriteMiddleware(app, static=static) (raw ASGI)
  • app.add_middleware(StaticRewriteMiddleware, static=static) (Starlette / FastAPI / Air)

Relates to #1.

Test plan

  • Two new tests verify the inner app's result propagates on both
    the HTTP and non-HTTP code paths
  • All 41 existing tests pass
  • Red/green TDD: tests failed before the fix, pass after

audreyfeldroy and others added 2 commits March 13, 2026 09:05
StaticRewriteMiddleware.__call__ now uses `return await self.app()`
on both code paths. Frameworks like Starlette, FastAPI, and Air
rely on return values propagating through the middleware stack when
using app.add_middleware(). A bare `await` without `return` silently
discards the result.

Co-authored-by: Daniel Roy Greenfeld <pydanny@users.noreply.github.com>
The ASGI spec is silent on return type, but Starlette and other
frameworks define ASGIApp as Awaitable[Any] because middleware
chains propagate return values via `return await self.app(...)`.
Widening from Awaitable[None] lets ty accept inner apps that
return non-None values, which is what the return-value propagation
tests exercise.

Also applies ruff formatting (import ordering, dict literal style)
to both files.
Copy link
Copy Markdown
Member

@pydanny pydanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pydanny pydanny merged commit ad9cdb5 into main Mar 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants