Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Released 2026-07-23
<https://github.com/pallets/flask/security/advisories/GHSA-4grg-w6v8-c28g>
- Support partitioned cookies
- Don't apply max_form_parts to non-multipart forms
- Allow assigning ASGI middleware to ``asgi_app`` without type errors.

## Version 0.20.0

Expand Down
50 changes: 29 additions & 21 deletions src/quart/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import NoReturn
from typing import overload
from typing import ParamSpec
from typing import TYPE_CHECKING
from typing import TypeVar
from urllib.parse import quote

Expand Down Expand Up @@ -94,6 +95,7 @@
from .testing import TestApp
from .typing import AfterServingCallable
from .typing import AfterWebsocketCallable
from .typing import ASGIApp
from .typing import ASGIHTTPProtocol
from .typing import ASGILifespanProtocol
from .typing import ASGIWebsocketProtocol
Expand Down Expand Up @@ -1735,31 +1737,37 @@ async def __call__(
"""
await self.asgi_app(scope, receive, send)

async def asgi_app(
self, scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable
) -> None:
"""This handles ASGI calls, it can be wrapped in middleware.
if TYPE_CHECKING:
asgi_app: ASGIApp
else:

When using middleware with Quart it is preferable to wrap this
method rather than the app itself. This is to ensure that the
app is an instance of this class - which allows the quart cli
to work correctly. To use this feature simply do,
async def asgi_app(
self, scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable
) -> None:
"""This handles ASGI calls, it can be wrapped in middleware.

.. code-block:: python
When using middleware with Quart it is preferable to wrap this
method rather than the app itself. This is to ensure that the
app is an instance of this class - which allows the quart cli
to work correctly. To use this feature simply do,

app.asgi_app = middleware(app.asgi_app)
.. code-block:: python

"""
asgi_handler: ASGIHTTPProtocol | ASGILifespanProtocol | ASGIWebsocketProtocol
if scope["type"] == "http":
asgi_handler = self.asgi_http_class(self, scope)
elif scope["type"] == "websocket":
asgi_handler = self.asgi_websocket_class(self, scope)
elif scope["type"] == "lifespan":
asgi_handler = self.asgi_lifespan_class(self, scope)
else:
raise RuntimeError("ASGI Scope type is unknown")
await asgi_handler(receive, send)
app.asgi_app = middleware(app.asgi_app)

"""
asgi_handler: (
ASGIHTTPProtocol | ASGILifespanProtocol | ASGIWebsocketProtocol
)
if scope["type"] == "http":
asgi_handler = self.asgi_http_class(self, scope)
elif scope["type"] == "websocket":
asgi_handler = self.asgi_websocket_class(self, scope)
elif scope["type"] == "lifespan":
asgi_handler = self.asgi_lifespan_class(self, scope)
else:
raise RuntimeError("ASGI Scope type is unknown")
await asgi_handler(receive, send)

async def startup(self) -> None:
self.shutdown_event = self.event_class()
Expand Down
1 change: 1 addition & 0 deletions src/quart/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .wrappers.response import Response # noqa: F401

FilePath = bytes | str | os.PathLike
ASGIApp = Callable[[Any, Any, Any], Awaitable[None]]

# The possible types that are directly convertible or are a Response object.
ResponseValue = Union[
Expand Down
2 changes: 1 addition & 1 deletion tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ async def __call__(
else:
await self.app(scope, receive, send)

app.asgi_app = OddMiddleware(app.asgi_app) # type: ignore
app.asgi_app = OddMiddleware(app.asgi_app)

client = app.test_client()
response = await client.get("/")
Expand Down
Loading