Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Allow combining non base-derived objects for retries #485

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@
---
features:
- |
Allow for callables to be combined as retry values. This will only
work when used combined with their corresponding implementation
retry objects, e.g. only async functions will work when used together
with async retry strategies.
59 changes: 55 additions & 4 deletions tenacity/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
import re
import typing

from . import _utils

try:
import tornado
except ImportError:
tornado = None

if typing.TYPE_CHECKING:
from tenacity import RetryCallState

Expand All @@ -30,15 +37,29 @@ def __call__(self, retry_state: "RetryCallState") -> bool:
pass

def __and__(self, other: "retry_base") -> "retry_all":
return other.__rand__(self)
if isinstance(other, retry_base):
# Delegate to the other object to allow for specific
# implementations, such as asyncio
return other.__rand__(self)
return retry_all(other, self)

def __rand__(self, other: "retry_base") -> "retry_all":
# This is automatically invoked for inheriting classes,
# so it helps to keep the abstraction and delegate specific
# implementations, such as asyncio
return retry_all(other, self)

def __or__(self, other: "retry_base") -> "retry_any":
return other.__ror__(self)
if isinstance(other, retry_base):
# Delegate to the other object to allow for specific
# implementations, such as asyncio
return other.__ror__(self)
return retry_any(other, self)

def __ror__(self, other: "retry_base") -> "retry_any":
# This is automatically invoked for inheriting classes,
# so it helps to keep the abstraction and delegate specific
# implementations, such as asyncio
return retry_any(other, self)


Expand Down Expand Up @@ -269,7 +290,22 @@ def __init__(self, *retries: retry_base) -> None:
self.retries = retries

def __call__(self, retry_state: "RetryCallState") -> bool:
return any(r(retry_state) for r in self.retries)
result = False
for r in self.retries:
if _utils.is_coroutine_callable(r) or (
tornado
and hasattr(tornado.gen, "is_coroutine_function")
and tornado.gen.is_coroutine_function(r)
):
raise TypeError(
"Cannot use async functions in a sync context. Make sure "
"you use the correct retrying object and the corresponding "
"async strategies"
)
result = result or r(retry_state)
if result:
break
return result


class retry_all(retry_base):
Expand All @@ -279,4 +315,19 @@ def __init__(self, *retries: retry_base) -> None:
self.retries = retries

def __call__(self, retry_state: "RetryCallState") -> bool:
return all(r(retry_state) for r in self.retries)
result = True
for r in self.retries:
if _utils.is_coroutine_callable(r) or (
tornado
and hasattr(tornado.gen, "is_coroutine_function")
and tornado.gen.is_coroutine_function(r)
):
raise TypeError(
"Cannot use async functions in a sync context. Make sure "
"you use the correct retrying object and the corresponding "
"async strategies"
)
result = result and r(retry_state)
if not result:
break
return result
Loading