Fix @datastar_response typing for Django async views#42
Open
MarcusL11 wants to merge 1 commit intostarfederation:developfrom
Open
Fix @datastar_response typing for Django async views#42MarcusL11 wants to merge 1 commit intostarfederation:developfrom
MarcusL11 wants to merge 1 commit intostarfederation:developfrom
Conversation
…wing The decorator's single union return type prevents mypy from narrowing sync vs async views, forcing users to cast() when passing to path(). Add @overload signatures using Coroutine (not Awaitable) to match what django-stubs expects for async view callables.
gazpachoking
reviewed
May 3, 2026
|
|
||
| @overload | ||
| def datastar_response( | ||
| func: Callable[P, Coroutine[Any, Any, DatastarEvents]], |
Collaborator
There was a problem hiding this comment.
Is the Coroutine here correct? Doesn't that eliminate async generators, which should be valid?
@datastar_response
async def async_gen():
yield SSE.patch_elements('<div id="aoeu"></div>')
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
@overloadsignatures to the Djangodatastar_responsedecorator so mypy can narrow the return type based on sync vs async inputCoroutine[Any, Any, ...]instead ofAwaitable[...]for the async overload, matching what django-stubspath()expectsProblem
The decorator currently has a single signature that returns a union type:
Mypy can't narrow this, so passing a decorated view to
path()fails:This forces users to wrap every decorated view in
cast():Why
Coroutineand notAwaitabledjango-stubs accepts async views as
Callable[..., Coroutine[Any, Any, HttpResponseBase]]. SinceCoroutineis a subtype ofAwaitable, using the broaderAwaitabledoesn't satisfy the narrowerCoroutineexpectation.Test plan
path()test_decorator_matrix.pyDjango tests pass (4/4)