Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Detect a ``functools.partial`` wrapping a callable object whose
``__call__`` is asynchronous as a coroutine callable. Previously such a
partial was misrouted to the synchronous retry path, which returned an
un-awaited coroutine instead of retrying the awaited call.
8 changes: 6 additions & 2 deletions tenacity/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ def is_coroutine_callable(call: typing.Callable[..., typing.Any]) -> bool:
return False
if inspect.iscoroutinefunction(call):
return True
partial_call = isinstance(call, functools.partial) and call.func
dunder_call = partial_call or getattr(call, "__call__", None) # noqa: B004
if isinstance(call, functools.partial):
# Recurse into the wrapped target so a partial over a callable *object*
# with an async ``__call__`` (not just a plain coroutine function) is
# still detected as a coroutine callable.
return is_coroutine_callable(call.func)
dunder_call = getattr(call, "__call__", None) # noqa: B004
return inspect.iscoroutinefunction(dunder_call)


Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __call__(self) -> None:
partial_sync_func = functools.partial(sync_func)
partial_async_class = functools.partial(AsyncClass().__call__)
partial_sync_class = functools.partial(SyncClass().__call__)
partial_async_instance = functools.partial(AsyncClass())
partial_sync_instance = functools.partial(SyncClass())
partial_lambda_fn = functools.partial(lambda_fn)

assert _utils.is_coroutine_callable(async_func) is True
Expand All @@ -38,6 +40,8 @@ def __call__(self) -> None:
assert _utils.is_coroutine_callable(partial_sync_func) is False
assert _utils.is_coroutine_callable(partial_async_class) is True
assert _utils.is_coroutine_callable(partial_sync_class) is False
assert _utils.is_coroutine_callable(partial_async_instance) is True
assert _utils.is_coroutine_callable(partial_sync_instance) is False
assert _utils.is_coroutine_callable(partial_lambda_fn) is False


Expand Down
Loading