From 85ece1c3e48e09a27de7dbd6a4a003d06f1a734a Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 27 Nov 2025 10:59:32 +0100 Subject: [PATCH 1/4] Fix #795: Incorrect typing with async decorators --- dramatiq/actor.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dramatiq/actor.py b/dramatiq/actor.py index 1d05208a..04b56e21 100644 --- a/dramatiq/actor.py +++ b/dramatiq/actor.py @@ -203,12 +203,17 @@ def __str__(self) -> str: @overload -def actor(fn: Callable[P, Union[Awaitable[R], R]], **kwargs) -> Actor[P, R]: +def actor(fn: Callable[P, Awaitable[R]], **kwargs) -> Actor[P, R]: pass @overload -def actor(fn: None = None, **kwargs) -> Callable[[Callable[P, Union[Awaitable[R], R]]], Actor[P, R]]: +def actor(fn: Callable[P, R], **kwargs) -> Actor[P, R]: + pass + + +@overload +def actor(fn: None = None, **kwargs) -> Union[Callable[[Callable[P, Awaitable[R]]], Actor[P, R]], Callable[[Callable[P, R]], Actor[P, R]]]: pass From 442a5030a95f4613528988d69c3757abe8b1cf64 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 27 Nov 2025 11:31:48 +0100 Subject: [PATCH 2/4] fix - add Protocol --- dramatiq/actor.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dramatiq/actor.py b/dramatiq/actor.py index 04b56e21..875c94d7 100644 --- a/dramatiq/actor.py +++ b/dramatiq/actor.py @@ -27,6 +27,7 @@ Generic, Optional, ParamSpec, + Protocol, TypeVar, Union, overload, @@ -202,6 +203,16 @@ def __str__(self) -> str: return "Actor(%(actor_name)s)" % vars(self) +class ActorDecorator(Protocol): + @overload + def __call__(self, f: Callable[P, R], **kwargs) -> Actor[P, R]: ... + + @overload + def __call__(self, f: Callable[P, Awaitable[R]], **kwargs) -> Actor[P, R]: ... + + def __call__(self, f: Callable[P, Union[Awaitable[R], R]], **kwargs) -> Actor[P, R]: ... + + @overload def actor(fn: Callable[P, Awaitable[R]], **kwargs) -> Actor[P, R]: pass @@ -213,7 +224,7 @@ def actor(fn: Callable[P, R], **kwargs) -> Actor[P, R]: @overload -def actor(fn: None = None, **kwargs) -> Union[Callable[[Callable[P, Awaitable[R]]], Actor[P, R]], Callable[[Callable[P, R]], Actor[P, R]]]: +def actor(fn: None = None, **kwargs) -> ActorDecorator: pass From 51abedea4931479246e3f0e68ee31e6555b3fd3c Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 27 Nov 2025 11:52:14 +0100 Subject: [PATCH 3/4] reorder protocol overloads --- dramatiq/actor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dramatiq/actor.py b/dramatiq/actor.py index 875c94d7..d543ff75 100644 --- a/dramatiq/actor.py +++ b/dramatiq/actor.py @@ -205,10 +205,10 @@ def __str__(self) -> str: class ActorDecorator(Protocol): @overload - def __call__(self, f: Callable[P, R], **kwargs) -> Actor[P, R]: ... + def __call__(self, f: Callable[P, Awaitable[R]], **kwargs) -> Actor[P, R]: ... @overload - def __call__(self, f: Callable[P, Awaitable[R]], **kwargs) -> Actor[P, R]: ... + def __call__(self, f: Callable[P, R], **kwargs) -> Actor[P, R]: ... def __call__(self, f: Callable[P, Union[Awaitable[R], R]], **kwargs) -> Actor[P, R]: ... From 13fb48d35610b189f6c7101874690dc0224cedcc Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 27 Nov 2025 11:54:47 +0100 Subject: [PATCH 4/4] fix --- dramatiq/actor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dramatiq/actor.py b/dramatiq/actor.py index d543ff75..c8186103 100644 --- a/dramatiq/actor.py +++ b/dramatiq/actor.py @@ -205,12 +205,12 @@ def __str__(self) -> str: class ActorDecorator(Protocol): @overload - def __call__(self, f: Callable[P, Awaitable[R]], **kwargs) -> Actor[P, R]: ... + def __call__(self, fn: Callable[P, Awaitable[R]]) -> Actor[P, R]: ... @overload - def __call__(self, f: Callable[P, R], **kwargs) -> Actor[P, R]: ... + def __call__(self, fn: Callable[P, R]) -> Actor[P, R]: ... - def __call__(self, f: Callable[P, Union[Awaitable[R], R]], **kwargs) -> Actor[P, R]: ... + def __call__(self, fn: Callable[P, Union[Awaitable[R], R]]) -> Actor[P, R]: ... @overload