diff --git a/backoff/_decorator.py b/backoff/_decorator.py index 6cf9e17..92dee1b 100644 --- a/backoff/_decorator.py +++ b/backoff/_decorator.py @@ -30,9 +30,9 @@ def on_predicate(wait_gen: _WaitGenerator, max_tries: Optional[_MaybeCallable[int]] = None, max_time: Optional[_MaybeCallable[float]] = None, jitter: Union[_Jitterer, None] = full_jitter, - on_success: Union[_Handler, Iterable[_Handler]] = None, - on_backoff: Union[_Handler, Iterable[_Handler]] = None, - on_giveup: Union[_Handler, Iterable[_Handler]] = None, + on_success: Union[_Handler, Iterable[_Handler], None] = None, + on_backoff: Union[_Handler, Iterable[_Handler], None] = None, + on_giveup: Union[_Handler, Iterable[_Handler], None] = None, logger: _MaybeLogger = 'backoff', backoff_log_level: int = logging.INFO, giveup_log_level: int = logging.ERROR, @@ -127,9 +127,9 @@ def on_exception(wait_gen: _WaitGenerator, max_time: Optional[_MaybeCallable[float]] = None, jitter: Union[_Jitterer, None] = full_jitter, giveup: _Predicate[Exception] = lambda e: False, - on_success: Union[_Handler, Iterable[_Handler]] = None, - on_backoff: Union[_Handler, Iterable[_Handler]] = None, - on_giveup: Union[_Handler, Iterable[_Handler]] = None, + on_success: Union[_Handler, Iterable[_Handler], None] = None, + on_backoff: Union[_Handler, Iterable[_Handler], None] = None, + on_giveup: Union[_Handler, Iterable[_Handler], None] = None, raise_on_giveup: bool = True, logger: _MaybeLogger = 'backoff', backoff_log_level: int = logging.INFO, diff --git a/backoff/_typing.py b/backoff/_typing.py index 67a624d..20446d4 100644 --- a/backoff/_typing.py +++ b/backoff/_typing.py @@ -1,11 +1,8 @@ # coding:utf-8 import logging import sys -from typing import (Any, Callable, Dict, Generator, Sequence, Tuple, Union, - TypeVar) - - -details_kwargs = {"total": False} +from typing import (Any, Callable, Coroutine, Dict, Generator, Sequence, Tuple, + TypeVar, Union) if sys.version_info >= (3, 8): # pragma: no cover from typing import TypedDict @@ -14,8 +11,9 @@ try: from typing_extensions import TypedDict except ImportError: - TypedDict = Dict[str, Any] - del details_kwargs["total"] + class TypedDict(dict): + def __init_subclass__(cls, **kwargs: Any) -> None: + return super().__init_subclass__() class _Details(TypedDict): @@ -26,7 +24,7 @@ class _Details(TypedDict): elapsed: float -class Details(_Details, **details_kwargs): +class Details(_Details, total=False): wait: float # present in the on_backoff handler case for either decorator value: Any # present in the on_predicate decorator case @@ -34,7 +32,10 @@ class Details(_Details, **details_kwargs): T = TypeVar("T") _CallableT = TypeVar('_CallableT', bound=Callable[..., Any]) -_Handler = Callable[[Details], None] +_Handler = Union[ + Callable[[Details], None], + Callable[[Details], Coroutine[Any, Any, None]], +] _Jitterer = Callable[[float], float] _MaybeCallable = Union[T, Callable[[], T]] _MaybeLogger = Union[str, logging.Logger, None]