Skip to content

Commit

Permalink
Fix type hint for wait generator
Browse files Browse the repository at this point in the history
I picked this up in one of the type hinting improvement PRs but it
doesn't seem to work and isn't necessary.

This also adds a test file which will be picked up by mypy when
running make check which would have caught the error.

Fixes #177
  • Loading branch information
bgreen-litl committed Oct 5, 2022
1 parent 9a20a7f commit 6cb1a4e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions backoff/_wait_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def expo(
base: float = 2,
factor: float = 1,
max_value: Optional[float] = None
) -> Generator[Optional[float], Any, None]:
) -> Generator[float, Any, None]:

"""Generator for exponential decay.
Expand Down Expand Up @@ -54,7 +54,7 @@ def fibo(max_value: Optional[int] = None) -> Generator[int, None, None]:

def constant(
interval: Union[int, Iterable[float]] = 1
) -> Generator[Optional[float], None, None]:
) -> Generator[float, None, None]:
"""Generator for constant intervals.
Args:
Expand All @@ -75,7 +75,7 @@ def constant(
def runtime(
*,
value: Callable[[Any], float]
) -> Generator[Optional[float], None, None]:
) -> Generator[float, None, None]:
"""Generator that is based on parsing the return value or thrown
exception of the decorated method
Expand Down
34 changes: 34 additions & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import backoff


# No pyunit tests are defined here yet, but the following decorator calls will
# be analyzed by mypy which would have caught a bug the last release.

@backoff.on_exception(
backoff.expo,
ValueError,
jitter=None,
max_tries=3,
)
def foo():
raise ValueError()


@backoff.on_exception(
backoff.constant,
ValueError,
interval=1,
max_tries=3
)
def bar():
raise ValueError()


@backoff.on_predicate(
backoff.runtime,
predicate=lambda r: r.status_code == 429,
value=lambda r: int(r.headers.get("Retry-After")),
jitter=None,
)
def baz():
pass

0 comments on commit 6cb1a4e

Please sign in to comment.