From 7610ffd85875a6d212dc8f9b7798181c5082f3a5 Mon Sep 17 00:00:00 2001 From: MarcusL11 Date: Sun, 3 May 2026 13:55:09 +0700 Subject: [PATCH] fix: add @overload to Django datastar_response for correct mypy narrowing 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. --- src/datastar_py/django.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/datastar_py/django.py b/src/datastar_py/django.py index 955a343..f3ce295 100644 --- a/src/datastar_py/django.py +++ b/src/datastar_py/django.py @@ -1,9 +1,9 @@ from __future__ import annotations -from collections.abc import Awaitable, Callable, Mapping +from collections.abc import Awaitable, Callable, Coroutine, Mapping from functools import wraps from inspect import isasyncgenfunction, isawaitable, iscoroutinefunction -from typing import Any, ParamSpec +from typing import Any, ParamSpec, overload from django.http import HttpRequest from django.http import StreamingHttpResponse as _StreamingHttpResponse @@ -44,6 +44,18 @@ def __init__( P = ParamSpec("P") +@overload +def datastar_response( + func: Callable[P, Coroutine[Any, Any, DatastarEvents]], +) -> Callable[P, Coroutine[Any, Any, DatastarResponse]]: ... + + +@overload +def datastar_response( + func: Callable[P, DatastarEvents], +) -> Callable[P, DatastarResponse]: ... + + def datastar_response( func: Callable[P, Awaitable[DatastarEvents] | DatastarEvents], ) -> Callable[P, Awaitable[DatastarResponse] | DatastarResponse]: