Skip to content

Commit 927965d

Browse files
committed
Copy over strategies as async
1 parent 3c5f788 commit 927965d

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

tenacity/asyncio/retry.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
import abc
17-
import inspect
1817
import typing
1918

2019
from tenacity import _utils
2120
from tenacity import retry_base
22-
from tenacity import retry_if_exception as _retry_if_exception
23-
from tenacity import retry_if_result as _retry_if_result
2421

2522
if typing.TYPE_CHECKING:
2623
from tenacity import RetryCallState
@@ -54,35 +51,48 @@ def __ror__( # type: ignore[misc,override]
5451
return retry_any(other, self)
5552

5653

57-
class async_predicate_mixin:
58-
async def __call__(self, retry_state: "RetryCallState") -> bool:
59-
result = super().__call__(retry_state) # type: ignore[misc]
60-
if inspect.isawaitable(result):
61-
result = await result
62-
return typing.cast(bool, result)
63-
64-
6554
RetryBaseT = typing.Union[
6655
async_retry_base, typing.Callable[["RetryCallState"], typing.Awaitable[bool]]
6756
]
6857

6958

70-
class retry_if_exception(async_predicate_mixin, _retry_if_exception, async_retry_base): # type: ignore[misc]
59+
class retry_if_exception(async_retry_base):
7160
"""Retry strategy that retries if an exception verifies a predicate."""
7261

7362
def __init__(
7463
self, predicate: typing.Callable[[BaseException], typing.Awaitable[bool]]
7564
) -> None:
76-
super().__init__(predicate) # type: ignore[arg-type]
65+
self.predicate = predicate
66+
67+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
68+
if retry_state.outcome is None:
69+
raise RuntimeError("__call__() called before outcome was set")
7770

71+
if retry_state.outcome.failed:
72+
exception = retry_state.outcome.exception()
73+
if exception is None:
74+
raise RuntimeError("outcome failed but the exception is None")
75+
return await self.predicate(exception)
76+
else:
77+
return False
7878

79-
class retry_if_result(async_predicate_mixin, _retry_if_result, async_retry_base): # type: ignore[misc]
79+
80+
class retry_if_result(async_retry_base):
8081
"""Retries if the result verifies a predicate."""
8182

8283
def __init__(
8384
self, predicate: typing.Callable[[typing.Any], typing.Awaitable[bool]]
8485
) -> None:
85-
super().__init__(predicate) # type: ignore[arg-type]
86+
self.predicate = predicate
87+
88+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
89+
if retry_state.outcome is None:
90+
raise RuntimeError("__call__() called before outcome was set")
91+
92+
if not retry_state.outcome.failed:
93+
return await self.predicate(retry_state.outcome.result())
94+
else:
95+
return False
8696

8797

8898
class retry_any(async_retry_base):

0 commit comments

Comments
 (0)