diff --git a/backoff/_typing.py b/backoff/_typing.py index a1e8a88..0159904 100644 --- a/backoff/_typing.py +++ b/backoff/_typing.py @@ -1,12 +1,35 @@ # coding:utf-8 import logging -from typing import Any, Callable, Dict, Generator, Sequence, Union, TypeVar - +import sys +from typing import (Any, Callable, Dict, Generator, Sequence, Tuple, Union, + TypeVar) T = TypeVar("T") +if sys.version_info >= (3, 8): + from typing import TypedDict +else: + try: + from typing_extensions import TypedDict + except ImportError: + TypedDict = object + + +class _Details(TypedDict): + target: Callable[..., Any] + args: Tuple[Any, ...] + kwargs: Dict[str, Any] + tries: int + elapsed: float + + +class Details(_Details, total=False): + wait: float # this key will be present in the on_backoff handler case for either decorator + value: Any # this key will be present in the on_predicate decorator case + + _CallableT = TypeVar('_CallableT', bound=Callable[..., Any]) -_Handler = Callable[[Dict[str, Any]], None] +_Handler = Callable[[Details], None] _Jitterer = Callable[[float], float] _MaybeCallable = Union[T, Callable[[], T]] _MaybeLogger = Union[str, logging.Logger] diff --git a/backoff/types.py b/backoff/types.py new file mode 100644 index 0000000..25f20a4 --- /dev/null +++ b/backoff/types.py @@ -0,0 +1,6 @@ +# coding:utf-8 +from ._typing import Details + +__all__ = [ + 'Details' +]