-
Notifications
You must be signed in to change notification settings - Fork 14
align datastar_response behavior with docs (tests, fixes, examples) #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gazpachoking
merged 7 commits into
starfederation:develop
from
mmacpherson:fix-async-generator-decorator
Apr 17, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f366a89
Add decorator matrix and integration tests
mmacpherson 987eece
Normalize decorator return to DatastarResponse
mmacpherson 2a524c7
Document decorator preserving sync handlers
mmacpherson 63cb03c
Refine decorator to preserve sync/async semantics
mmacpherson add7b8d
respond to reviewer feedback:
mmacpherson ca51009
respond to review: restore Quart context, allow Django async generato…
mmacpherson 2580f02
fix pre-commit lint failures
mmacpherson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .PHONY: test lint | ||
|
|
||
| test: | ||
| uv run --dev pytest | ||
|
|
||
| lint: | ||
| uv run --dev pre-commit run --all-files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Runtime regression test for datastar_response: sync handlers must not stall the event loop.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
| import time | ||
| from typing import Any | ||
|
|
||
| import anyio | ||
| import httpx | ||
| import pytest | ||
| import uvicorn | ||
| from starlette.applications import Starlette | ||
| from starlette.responses import PlainTextResponse | ||
| from starlette.routing import Route | ||
|
|
||
| from datastar_py.sse import ServerSentEventGenerator as SSE | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def anyio_backend() -> str: | ||
| """Limit anyio plugin to asyncio backend for these tests.""" | ||
| return "asyncio" | ||
|
|
||
|
|
||
| async def _fetch( | ||
| client: httpx.AsyncClient, path: str, timings: dict[str, float], key: str | ||
| ) -> None: | ||
| start = time.perf_counter() | ||
| resp = await client.get(path, timeout=5.0) | ||
| timings[key] = time.perf_counter() - start | ||
| resp.raise_for_status() | ||
|
|
||
|
|
||
| @pytest.mark.anyio("asyncio") | ||
| async def test_sync_handler_runs_off_event_loop() -> None: | ||
| """Sync routes should stay in the threadpool; otherwise they block the event loop.""" | ||
| entered = threading.Event() | ||
|
|
||
| from datastar_py.starlette import datastar_response | ||
|
|
||
| @datastar_response | ||
| def slow(request) -> Any: | ||
| entered.set() | ||
| time.sleep(1.0) # if run on the event loop, this blocks other requests | ||
| return SSE.patch_signals({"slow": True}) | ||
|
|
||
| async def ping(request) -> PlainTextResponse: | ||
| return PlainTextResponse("pong") | ||
|
|
||
| app = Starlette(routes=[Route("/slow", slow), Route("/ping", ping)]) | ||
|
|
||
| config = uvicorn.Config(app, host="127.0.0.1", port=0, log_level="warning", lifespan="off") | ||
| server = uvicorn.Server(config) | ||
| thread = threading.Thread(target=server.run, daemon=True) | ||
| thread.start() | ||
|
|
||
| try: | ||
| # Wait for server to start and expose sockets | ||
| for _ in range(50): | ||
| if server.started and getattr(server, "servers", None): | ||
| break | ||
| await anyio.sleep(0.05) | ||
| else: | ||
| pytest.fail("Server did not start") | ||
|
|
||
| sock = server.servers[0].sockets[0] | ||
| host, port = sock.getsockname()[:2] | ||
| base_url = f"http://{host}:{port}" | ||
|
|
||
| async with httpx.AsyncClient(base_url=base_url) as client: | ||
| timings: dict[str, float] = {} | ||
| async with anyio.create_task_group() as tg: | ||
| tg.start_soon(_fetch, client, "/slow", timings, "slow") | ||
| await anyio.to_thread.run_sync(entered.wait, 1.0) | ||
| tg.start_soon(_fetch, client, "/ping", timings, "ping") | ||
|
|
||
| assert timings["slow"] >= 0.9 | ||
| assert timings["ping"] < 0.3, "Ping should not be blocked by slow sync handler" | ||
| finally: | ||
| server.should_exit = True | ||
| thread.join(timeout=2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we need the copy_current_request_context anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restored in ca51009. copy_current_request_context binds the handler to a copy of the current request context so quart.request / g stay accessible across the internal await. Left the sync branch alone for minimal diff.