Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Fixed

- Measure elapsed time after function call [#187](https://github.com/python-backoff/backoff/pull/187)
- Updated the type annotation of the `exception` argument in `on_exception`, `on_predicate`, `retry_context` and `aretry_context` to correctly expect an exception type or a tuple of exception types [#192](https://github.com/python-backoff/backoff/pull/192)

### Documentation

Expand Down
10 changes: 5 additions & 5 deletions backoff/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
_Handler,
_Jitterer,
_MaybeCallable,
_MaybeSequence,
_MaybeTuple,
_Predicate,
_WaitGenerator,
)
Expand Down Expand Up @@ -159,7 +159,7 @@ async def retry(*args: P.args, **kwargs: P.kwargs) -> T:
def retry_exception(
target: Callable[P, T],
wait_gen: _WaitGenerator,
exception: _MaybeSequence[type[Exception]],
exception: _MaybeTuple[type[Exception]],
*,
max_tries: _MaybeCallable[int] | None,
max_time: _MaybeCallable[float] | None,
Expand Down Expand Up @@ -203,7 +203,7 @@ async def retry(

try:
ret = await target(*args, **kwargs) # type: ignore[misc] # ty:ignore[invalid-await]
except exception as e: # type: ignore[misc] # ty:ignore[invalid-exception-caught]
except exception as e:
details["elapsed"] = state.record_elapsed()
giveup_result = await giveup(e)

Expand Down Expand Up @@ -248,7 +248,7 @@ async def _dispatch_handlers(


async def aretry_context(
exception: _MaybeSequence[type[Exception]],
exception: _MaybeTuple[type[Exception]],
wait_gen: _WaitGenerator,
*,
max_tries: _MaybeCallable[int] | None,
Expand All @@ -271,7 +271,7 @@ async def aretry_context(
)
while True:
state.start_attempt()
attempt = _Attempt(exception) # type: ignore[arg-type] # ty:ignore[invalid-argument-type]
attempt = _Attempt(exception)
yield attempt
elapsed = state.record_elapsed()

Expand Down
8 changes: 4 additions & 4 deletions backoff/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
_Jitterer,
_MaybeCallable,
_MaybeLogger,
_MaybeSequence,
_MaybeTuple,
_Predicate,
_WaitGenerator,
)
Expand Down Expand Up @@ -143,7 +143,7 @@ def decorate(target: Callable[P, T]) -> Callable[P, T]:

def on_exception(
wait_gen: _WaitGenerator,
exception: _MaybeSequence[type[Exception]],
exception: _MaybeTuple[type[Exception]],
*,
max_tries: _MaybeCallable[int] | None = None,
max_time: _MaybeCallable[float] | None = None,
Expand Down Expand Up @@ -247,7 +247,7 @@ def decorate(target: Callable[P, T]) -> Callable[P, T]:


def retry_context(
exception: _MaybeSequence[type[Exception]] = Exception,
exception: _MaybeTuple[type[Exception]] = Exception,
wait_gen: _WaitGenerator = expo,
*,
max_tries: _MaybeCallable[int] | None = None,
Expand Down Expand Up @@ -343,7 +343,7 @@ def retry_context(


def aretry_context(
exception: _MaybeSequence[type[Exception]] = Exception,
exception: _MaybeTuple[type[Exception]] = Exception,
wait_gen: _WaitGenerator = expo,
*,
max_tries: _MaybeCallable[int] | None = None,
Expand Down
10 changes: 5 additions & 5 deletions backoff/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
_Handler,
_Jitterer,
_MaybeCallable,
_MaybeSequence,
_MaybeTuple,
_Predicate,
_WaitGenerator,
)
Expand Down Expand Up @@ -117,7 +117,7 @@ def retry(*args: P.args, **kwargs: P.kwargs) -> T:
def retry_exception(
target: Callable[P, T],
wait_gen: _WaitGenerator,
exception: _MaybeSequence[type[Exception]],
exception: _MaybeTuple[type[Exception]],
*,
max_tries: _MaybeCallable[int] | None,
max_time: _MaybeCallable[float] | None,
Expand Down Expand Up @@ -149,7 +149,7 @@ def retry(*args: P.args, **kwargs: P.kwargs) -> T: # type: ignore[return] # ty

try:
ret = target(*args, **kwargs)
except exception as e: # type: ignore[misc] # ty:ignore[invalid-exception-caught]
except exception as e:
details["elapsed"] = state.record_elapsed()

if giveup(e) or state.exhausted():
Expand Down Expand Up @@ -177,7 +177,7 @@ def retry(*args: P.args, **kwargs: P.kwargs) -> T: # type: ignore[return] # ty


def retry_context(
exception: _MaybeSequence[type[Exception]],
exception: _MaybeTuple[type[Exception]],
wait_gen: _WaitGenerator,
*,
max_tries: _MaybeCallable[int] | None,
Expand All @@ -198,7 +198,7 @@ def retry_context(
)
while True:
state.start_attempt()
attempt = _Attempt(exception) # type: ignore[arg-type] # ty:ignore[invalid-argument-type]
attempt = _Attempt(exception)
yield attempt
elapsed = state.record_elapsed()

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 @@
from __future__ import annotations

import logging
from collections.abc import Coroutine, Generator, Sequence
from collections.abc import Coroutine, Generator
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -60,7 +60,7 @@ class ContextDetails(_BaseContextDetails, _ContextCallDetails, total=False):
_Jitterer = Callable[[float], float]
_MaybeCallable = Union[T, Callable[[], T]]
_MaybeLogger = Union[str, logging.Logger, logging.LoggerAdapter, None]
_MaybeSequence = Union[T, Sequence[T]]
_MaybeTuple = Union[T, tuple[T, ...]]
_Predicate = Union[
Callable[[T], bool],
Callable[[T], Coroutine[Any, Any, bool]],
Expand Down