Skip to content

Commit

Permalink
Revert to single-dict type with optional keys
Browse files Browse the repository at this point in the history
The additional mixed-in types added too much complexity and maintenance effort while still requiring the user to be aware of use-case specific keys.
  • Loading branch information
leonhard-s committed Jul 17, 2021
1 parent cd50fbe commit e0cd03e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 100 deletions.
11 changes: 4 additions & 7 deletions backoff/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
from backoff._typing import (
_CallableT,
_Handler,
_HandlerBackoff,
_HandlerPredicate,
_HandlerPredicateBackoff,
_Jitterer,
_MaybeCallable,
_MaybeLogger,
Expand All @@ -33,9 +30,9 @@ def on_predicate(wait_gen: _WaitGenerator,
max_tries: Optional[_MaybeCallable[int]] = None,
max_time: Optional[_MaybeCallable[float]] = None,
jitter: _Jitterer = full_jitter,
on_success: Optional[_HandlerPredicate] = None,
on_backoff: Optional[_HandlerPredicateBackoff] = None,
on_giveup: Optional[_HandlerPredicate] = None,
on_success: Optional[_Handler] = None,
on_backoff: Optional[_Handler] = None,
on_giveup: Optional[_Handler] = None,
logger: _MaybeLogger = 'backoff',
backoff_log_level: int = logging.INFO,
giveup_log_level: int = logging.ERROR,
Expand Down Expand Up @@ -131,7 +128,7 @@ def on_exception(wait_gen: _WaitGenerator,
jitter: _Jitterer = full_jitter,
giveup: _Predicate[Exception] = lambda e: False,
on_success: Optional[_Handler] = None,
on_backoff: Optional[_HandlerBackoff] = None,
on_backoff: Optional[_Handler] = None,
on_giveup: Optional[_Handler] = None,
logger: _MaybeLogger = 'backoff',
backoff_log_level: int = logging.INFO,
Expand Down
104 changes: 13 additions & 91 deletions backoff/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,34 @@
import logging
import sys
from typing import (Any, Callable, Dict, Generator, Sequence, Tuple, Union,
TYPE_CHECKING, TypeVar)
TypeVar)

T = TypeVar("T")

if sys.version_info >= (3, 8):
from typing import TypedDict
elif TYPE_CHECKING:
else:
try:
from typing_extensions import TypedDict
except ImportError:
pass
try:
_ = TypedDict
except NameError:
Details = DetailsPredicate = Dict[str, Any] # type: ignore
DetailsBackoff = DetailsPredicateBackoff = Dict[str, Any] # type: ignore
else:
class Details(TypedDict):
"""Invocation details for a handler.
The following keys are valid for the `on_exception` decorator,
except for the `on_backoff` handler, which uses the
`DetailsBackoff` type instead.
Available keys:
`target` : reference to the function or method being invoked
`args` : positional arguments to func
`kwargs` : keyword arguments to func
`tries` : number of invocation tries so far
`elapsed`: elapsed time in seconds so far
`wait` : seconds to wait
"""

target: Callable[..., Any]
args: Tuple[Any, ...]
kwargs: Dict[str, Any]
tries: int
elapsed: float

class _DetailsBackoffMixin(TypedDict):
"""Extra keys specific to `on_backoff` handlers."""

wait: float

class DetailsBackoff(Details, _DetailsBackoffMixin):
"""Invocation details for a backoff handler.
The following keys are valid for the `on_exception` decorator,
and only for the `on_backoff` handler. Other handlers use the
`Details` type instead.
Available keys:
`target` : reference to the function or method being invoked
`args` : positional arguments to func
`kwargs` : keyword arguments to func
`tries` : number of invocation tries so far
`elapsed`: elapsed time in seconds so far
`wait` : seconds to wait
"""

class DetailsPredicate(Details):
"""Invocation details for a predicate handlers.
The following keys are valid for the `on_predicate` decorator,
except for the `on_backoff` handler, which uses the
`DetailsPredicateBackoff` type instead.
Available keys:
`target` : reference to the function or method being invoked
`args` : positional arguments to func
`kwargs` : keyword arguments to func
`tries` : number of invocation tries so far
`elapsed`: elapsed time in seconds so far
`value` : value triggering backoff
"""

value: Any
TypedDict = object

class DetailsPredicateBackoff(DetailsPredicate, _DetailsBackoffMixin):
"""Invocation details for a predicate backoff handlers.

The following keys are valid for the `on_predicate` decorator,
and only for the `on_backoff` handler. Other handlers use the
`DetailsPredicate` type instead.
class _Details(TypedDict):
target: Callable[..., Any]
args: Tuple[Any, ...]
kwargs: Dict[str, Any]
tries: int
elapsed: float

Available keys:

`target` : reference to the function or method being invoked
`args` : positional arguments to func
`kwargs` : keyword arguments to func
`tries` : number of invocation tries so far
`elapsed`: elapsed time in seconds so far
`wait` : seconds to wait
`value` : value triggering backoff
"""
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


_HandlerT = Callable[[T], None]
_CallableT = TypeVar('_CallableT', bound=Callable[..., Any])
_HandlerPredicate = _HandlerT[DetailsPredicate]
_HandlerPredicateBackoff = _HandlerT[DetailsPredicateBackoff]
_Handler = _HandlerT[Details]
_HandlerBackoff = _HandlerT[DetailsBackoff]
_Handler = Callable[[Details], None]
_Jitterer = Callable[[float], float]
_MaybeCallable = Union[T, Callable[[], T]]
_MaybeLogger = Union[str, logging.Logger]
Expand Down
7 changes: 5 additions & 2 deletions backoff/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# coding:utf-8
from ._typing import (Details, DetailsBackoff,
DetailsPredicate, DetailsPredicateBackoff)
from ._typing import Details

__all__ = [
'Details'
]

0 comments on commit e0cd03e

Please sign in to comment.