Skip to content
This repository was archived by the owner on Aug 8, 2025. It is now read-only.

Commit 6cb1a4e

Browse files
committed
Fix type hint for wait generator
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
1 parent 9a20a7f commit 6cb1a4e

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

backoff/_wait_gen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def expo(
88
base: float = 2,
99
factor: float = 1,
1010
max_value: Optional[float] = None
11-
) -> Generator[Optional[float], Any, None]:
11+
) -> Generator[float, Any, None]:
1212

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

5555
def constant(
5656
interval: Union[int, Iterable[float]] = 1
57-
) -> Generator[Optional[float], None, None]:
57+
) -> Generator[float, None, None]:
5858
"""Generator for constant intervals.
5959
6060
Args:
@@ -75,7 +75,7 @@ def constant(
7575
def runtime(
7676
*,
7777
value: Callable[[Any], float]
78-
) -> Generator[Optional[float], None, None]:
78+
) -> Generator[float, None, None]:
7979
"""Generator that is based on parsing the return value or thrown
8080
exception of the decorated method
8181

tests/test_typing.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import backoff
2+
3+
4+
# No pyunit tests are defined here yet, but the following decorator calls will
5+
# be analyzed by mypy which would have caught a bug the last release.
6+
7+
@backoff.on_exception(
8+
backoff.expo,
9+
ValueError,
10+
jitter=None,
11+
max_tries=3,
12+
)
13+
def foo():
14+
raise ValueError()
15+
16+
17+
@backoff.on_exception(
18+
backoff.constant,
19+
ValueError,
20+
interval=1,
21+
max_tries=3
22+
)
23+
def bar():
24+
raise ValueError()
25+
26+
27+
@backoff.on_predicate(
28+
backoff.runtime,
29+
predicate=lambda r: r.status_code == 429,
30+
value=lambda r: int(r.headers.get("Retry-After")),
31+
jitter=None,
32+
)
33+
def baz():
34+
pass

0 commit comments

Comments
 (0)