From 03d11c4cd89bdf8f4e83334d252cc0fdfa13a67b Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 13 Mar 2026 11:19:45 +0000 Subject: [PATCH] typing: Fix signature of Redis.transaction The return type depends on the value of the value_from_callable kwarg. Indicate this with type hinting. Signed-off-by: Stephen Finucane --- redis/client.py | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/redis/client.py b/redis/client.py index c42d231901..368196e593 100755 --- a/redis/client.py +++ b/redis/client.py @@ -10,11 +10,14 @@ Callable, Dict, List, + Literal, Mapping, Optional, Set, Type, + TypeVar, Union, + overload, ) from redis._parsers.encoders import Encoder @@ -94,6 +97,9 @@ logger = logging.getLogger(__name__) +T = TypeVar('T') + + def is_debug_log_enabled(): return logger.isEnabledFor(logging.DEBUG) @@ -539,17 +545,42 @@ def pipeline(self, transaction=True, shard_hint=None) -> "Pipeline": self.connection_pool, self.response_callbacks, transaction, shard_hint ) + @overload + def transaction( + self, + func: Callable[["Pipeline"], T], + *watches: str | None, + shard_hint: Any = None, + value_from_callable: Literal[False] = False, + watch_delay: int | None = None, + **kwargs: Any, + ) -> List[Any]: ... + + @overload + def transaction( + self, + func: Callable[["Pipeline"], T], + *watches: str | None, + shard_hint: Any = None, + value_from_callable: Literal[True] = ..., + watch_delay: int | None = None, + **kwargs: Any, + ) -> T: ... + def transaction( - self, func: Callable[["Pipeline"], None], *watches, **kwargs - ) -> Union[List[Any], Any, None]: + self, + func: Callable[["Pipeline"], T], + *watches: str | None, + shard_hint: Any = None, + value_from_callable: bool = False, + watch_delay: int | None = None, + **kwargs: Any, + ) -> Union[List[Any], T]: """ Convenience method for executing the callable `func` as a transaction while watching all keys specified in `watches`. The 'func' callable should expect a single argument which is a Pipeline object. """ - shard_hint = kwargs.pop("shard_hint", None) - value_from_callable = kwargs.pop("value_from_callable", False) - watch_delay = kwargs.pop("watch_delay", None) with self.pipeline(True, shard_hint) as pipe: while True: try: