diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e0c1bc..c897f35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/backoff/_typing.py b/backoff/_typing.py index f013913..a975e65 100644 --- a/backoff/_typing.py +++ b/backoff/_typing.py @@ -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]] diff --git a/backoff/_wait_gen.py b/backoff/_wait_gen.py index 7a88470..1bac7bf 100644 --- a/backoff/_wait_gen.py +++ b/backoff/_wait_gen.py @@ -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: @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 3b75104..9ae5014 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] @@ -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 diff --git a/tests/test_wait_gen.py b/tests/test_wait_gen.py index 3e8d1b8..5a30fff 100644 --- a/tests/test_wait_gen.py +++ b/tests/test_wait_gen.py @@ -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)