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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

- Check types with [`ty`](https://docs.astral.sh/ty/) and update some type annotations [#179](https://github.com/python-backoff/backoff/pull/179)

- Simplify `backoff.constant` wait generator implementation [#180](https://github.com/python-backoff/backoff/pull/180)

## [v2.3.1] - 2025-12-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion backoff/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ class Details(_Details, total=False):
_MaybeLogger = Union[str, logging.Logger, logging.LoggerAdapter, None]
_MaybeSequence = Union[T, Sequence[T]]
_Predicate = Union[Callable[[T], bool], Callable[[T], Coroutine[Any, Any, bool]]]
_WaitGenerator = Callable[..., Generator[Union[float, None], None, None]]
_WaitGenerator = Callable[..., Generator[Union[float, None], Union[int, None], None]]
17 changes: 9 additions & 8 deletions backoff/_wait_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def fibo(max_value: int | None = None) -> Generator[int | None, None, None]:


def constant(
interval: int | Iterable[float] = 1,
) -> Generator[int | float | None, None, None]:
interval: float | Iterable[float] = 1,
) -> Generator[int | float | None, int | None, None]:
"""Generator for constant intervals.

Args:
Expand All @@ -94,19 +94,20 @@ def constant(
# Advance past initial .send() call
yield # type: ignore[misc]

try:
itr = iter(interval) # type: ignore
except TypeError:
itr = itertools.repeat(interval) # type: ignore[arg-type]
itr = (
itertools.repeat(interval)
if isinstance(interval, (int, float))
else iter(interval)
)

for val in itr:
yield val # ty:ignore[invalid-yield]
yield val


def runtime(
*,
value: Callable[[Any], float],
) -> Generator[float | None, None, None]:
) -> Generator[float | None, int | None, None]:
"""Generator that is based on parsing the return value or thrown
exception of the decorated method

Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ typing = [
{ include-group = "test" },
"mypy>=0.942",
"ty>=0.0.59",
"types-requests>=2.27.20",
]

[tool.hatch.build.targets.sdist]
Expand Down Expand Up @@ -202,6 +201,12 @@ ignore = [
"UP028",
]

[tool.mypy]
follow_untyped_imports = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

[tool.ruff.lint.per-file-ignores]
"**/doccmd_*.py" = [
"F811", # redefinition of unused
Expand Down
9 changes: 8 additions & 1 deletion tests/test_wait_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,16 @@ def test_constant():
for _i in range(9):
assert next(gen) == 3

gen = backoff.constant(interval=[1, 2.0, 3.25])
gen.send(None)
assert next(gen) == 1
assert next(gen) == 2.0
assert next(gen) == 3.25
assert next(gen, None) is None


def test_runtime():
gen = backoff.runtime(value=lambda x: x)
gen.send(None)
for i in range(20):
assert i == gen.send(i) # ty:ignore[invalid-argument-type]
assert i == gen.send(i)
Loading