From a2841f71d8448d6dfe08feb2c27108967aceb4cf Mon Sep 17 00:00:00 2001 From: David Slater Date: Thu, 9 Feb 2023 15:41:40 -0800 Subject: [PATCH 1/4] added new typing and changed error response --- backoff/_decorator.py | 20 ++++++++++++++++++-- backoff/_typing.py | 2 +- tests/test_typing.py | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/backoff/_decorator.py b/backoff/_decorator.py index 77ed8c2..c281798 100644 --- a/backoff/_decorator.py +++ b/backoff/_decorator.py @@ -18,7 +18,7 @@ _Jitterer, _MaybeCallable, _MaybeLogger, - _MaybeSequence, + _MaybeTuple, _Predicate, _WaitGenerator, ) @@ -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, @@ -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 diff --git a/backoff/_typing.py b/backoff/_typing.py index 20446d4..cc9e6f5 100644 --- a/backoff/_typing.py +++ b/backoff/_typing.py @@ -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]] _Predicate = Callable[[T], bool] _WaitGenerator = Callable[..., Generator[float, None, None]] diff --git a/tests/test_typing.py b/tests/test_typing.py index 7f53459..8a657c2 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -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 \ No newline at end of file From 9d71e8b0ae2a991a4ebff4125f0d077bdb7360ef Mon Sep 17 00:00:00 2001 From: David Slater Date: Thu, 9 Feb 2023 15:42:59 -0800 Subject: [PATCH 2/4] add newline --- tests/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_typing.py b/tests/test_typing.py index 8a657c2..2a58722 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -45,4 +45,4 @@ def baz(): backoff.on_exception(backoff.expo, exception) raise AssertionError(f"Expected TypeError for {exception}") except TypeError: - pass \ No newline at end of file + pass From 9ed616abea5ddb1736a85681e9a4832e0c669972 Mon Sep 17 00:00:00 2001 From: David Slater Date: Thu, 9 Feb 2023 15:43:58 -0800 Subject: [PATCH 3/4] sequence not used --- backoff/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backoff/_typing.py b/backoff/_typing.py index cc9e6f5..66492b8 100644 --- a/backoff/_typing.py +++ b/backoff/_typing.py @@ -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 From e68d3f9971d0ce2f5d949d66932fbe48b1fec420 Mon Sep 17 00:00:00 2001 From: David Slater Date: Thu, 2 May 2024 14:50:06 -0700 Subject: [PATCH 4/4] Update backoff/_typing.py Add ellipsis for 2 more exceptions. --- backoff/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backoff/_typing.py b/backoff/_typing.py index 66492b8..cce37d1 100644 --- a/backoff/_typing.py +++ b/backoff/_typing.py @@ -39,6 +39,6 @@ class Details(_Details, total=False): _Jitterer = Callable[[float], float] _MaybeCallable = Union[T, Callable[[], T]] _MaybeLogger = Union[str, logging.Logger, None] -_MaybeTuple = Union[T, Tuple[T]] +_MaybeTuple = Union[T, Tuple[T, ...]] _Predicate = Callable[[T], bool] _WaitGenerator = Callable[..., Generator[float, None, None]]