Skip to content

Fix @datastar_response typing for Django async views#42

Open
MarcusL11 wants to merge 1 commit intostarfederation:developfrom
MarcusL11:fix/django-decorator-overload-typing
Open

Fix @datastar_response typing for Django async views#42
MarcusL11 wants to merge 1 commit intostarfederation:developfrom
MarcusL11:fix/django-decorator-overload-typing

Conversation

@MarcusL11
Copy link
Copy Markdown

Summary

  • Add @overload signatures to the Django datastar_response decorator so mypy can narrow the return type based on sync vs async input
  • Use Coroutine[Any, Any, ...] instead of Awaitable[...] for the async overload, matching what django-stubs path() expects

Problem

The decorator currently has a single signature that returns a union type:

Callable[P, Awaitable[DatastarResponse] | DatastarResponse]

Mypy can't narrow this, so passing a decorated view to path() fails:

error: Argument 2 to "path" has incompatible type
  "Callable[[HttpRequest], Awaitable[DatastarResponse] | DatastarResponse]";
  expected "Callable[..., HttpResponseBase]"

This forces users to wrap every decorated view in cast():

path("my-view/", cast(ViewCallable, my_view))

Why Coroutine and not Awaitable

django-stubs accepts async views as Callable[..., Coroutine[Any, Any, HttpResponseBase]]. Since Coroutine is a subtype of Awaitable, using the broader Awaitable doesn't satisfy the narrower Coroutine expectation.

Test plan

  • mypy passes for all 4 decorator variants (async/sync × value/generator) in path()
  • Existing test_decorator_matrix.py Django tests pass (4/4)

…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.
Comment thread src/datastar_py/django.py

@overload
def datastar_response(
func: Callable[P, Coroutine[Any, Any, DatastarEvents]],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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>')

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