Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new typing and changed error response #195

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions backoff/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
_Jitterer,
_MaybeCallable,
_MaybeLogger,
_MaybeSequence,
_MaybeTuple,
_Predicate,
_WaitGenerator,
)
Expand Down Expand Up @@ -120,8 +120,23 @@ def decorate(target):
return decorate


def _check_exception_type(exception: _MaybeTuple[Type[Exception]]) -> None:
"""
Raise TypeError if exception is not a valid type, otherwise return None
"""
if isinstance(exception, Type) and issubclass(exception, Exception):
return
if isinstance(exception, tuple) and all(isinstance(e, Type)
and issubclass(e, Exception) for e in exception):
return
raise TypeError(
f"exception '{exception}' of type {type(exception)} is not"
" an Exception type or a tuple of Exception types"
)


def on_exception(wait_gen: _WaitGenerator,
exception: _MaybeSequence[Type[Exception]],
exception: _MaybeTuple[Type[Exception]],
*,
max_tries: Optional[_MaybeCallable[int]] = None,
max_time: Optional[_MaybeCallable[float]] = None,
Expand Down Expand Up @@ -180,6 +195,7 @@ def on_exception(wait_gen: _WaitGenerator,
args will first be evaluated and their return values passed.
This is useful for runtime configuration.
"""
_check_exception_type(exception)
def decorate(target):
nonlocal logger, on_success, on_backoff, on_giveup

Expand Down
4 changes: 2 additions & 2 deletions backoff/_typing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding:utf-8
import logging
import sys
from typing import (Any, Callable, Coroutine, Dict, Generator, Sequence, Tuple,
from typing import (Any, Callable, Coroutine, Dict, Generator, Tuple,
TypeVar, Union)

if sys.version_info >= (3, 8): # pragma: no cover
Expand Down Expand Up @@ -39,6 +39,6 @@ class Details(_Details, total=False):
_Jitterer = Callable[[float], float]
_MaybeCallable = Union[T, Callable[[], T]]
_MaybeLogger = Union[str, logging.Logger, None]
_MaybeSequence = Union[T, Sequence[T]]
_MaybeTuple = Union[T, Tuple[T]]
davidslater marked this conversation as resolved.
Show resolved Hide resolved
davidslater marked this conversation as resolved.
Show resolved Hide resolved
_Predicate = Callable[[T], bool]
_WaitGenerator = Callable[..., Generator[float, None, None]]
14 changes: 14 additions & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ def bar():
)
def baz():
pass


# Type Successes
for exception in OSError, tuple([OSError]), (OSError, ValueError):
backoff.on_exception(backoff.expo, exception)


# Type Failures
for exception in OSError(), [OSError], (OSError, ValueError()), "hi", (2, 3):
try:
backoff.on_exception(backoff.expo, exception)
raise AssertionError(f"Expected TypeError for {exception}")
except TypeError:
pass