From 10a18035299b352fd35976fadc55f174d83caef0 Mon Sep 17 00:00:00 2001 From: Ben Mezger Date: Sat, 7 Oct 2023 17:54:53 +0200 Subject: [PATCH] Share parameters between retry decorator and classes --- pyretries/retry.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/pyretries/retry.py b/pyretries/retry.py index 416adae..23d778e 100644 --- a/pyretries/retry.py +++ b/pyretries/retry.py @@ -376,7 +376,14 @@ def __call__( return state.returned_value -def retry(strategies: t.Sequence[Strategy]): +def retry( + strategies: t.Sequence[Strategy] = [], + on_exceptions: t.Sequence[type[Exception]] | None = None, + before_hooks: t.Sequence[BeforeHookFuncT] | None = None, + after_hooks: t.Sequence[AfterHookFuncT[ReturnT]] | None = None, + retry_exception_hook: RetryExceptionCallHook | None = None, + should_log: bool = True, +): """ Retry decorator. Works both for sync and async functions @@ -389,7 +396,12 @@ def retry(strategies: t.Sequence[Strategy]): True Args: - strategies: A sequence of retry strategies + strategies: Sequence of retry strategies + on_exceptions: Sequence of exceptions to apply a retry strategy. + before_hooks: Hooks to run before running `func`. Runs Before strategy. + after_hooks: Hooks to run after running `func`. Runs before strategy. + retry_exception_hook: Hook to run when `func` raised an exception. Runs before strategy. + should_log: Specifies whether retry should log actions Returns: func (FuncT[ReturnT]): Functions return value or exception @@ -401,9 +413,23 @@ def decorator_retry( @functools.wraps(func) def wrapper_retry(*args, **kwargs): if inspect.iscoroutinefunction(func): - return AsyncRetry(strategies=strategies)(func, *args, **kwargs) - - return Retry(strategies=strategies)(func, *args, **kwargs) + return AsyncRetry( + strategies=strategies, + on_exceptions=on_exceptions, + before_hooks=before_hooks, + after_hooks=after_hooks, + retry_exception_hook=retry_exception_hook, + should_log=should_log, + )(func, *args, **kwargs) + + return Retry( + strategies=strategies, + on_exceptions=on_exceptions, + before_hooks=before_hooks, + after_hooks=after_hooks, + retry_exception_hook=retry_exception_hook, + should_log=should_log, + )(func, *args, **kwargs) return t.cast(FuncT[ReturnT], wrapper_retry)