Skip to content
Draft
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
14 changes: 1 addition & 13 deletions backoff/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import time
import traceback
import warnings
from typing import TYPE_CHECKING, Any, Callable, TypeVar

if TYPE_CHECKING:
Expand Down Expand Up @@ -64,18 +63,7 @@ def _next_wait(
max_time: float | None,
) -> float:
value = wait.send(send_value)
try:
seconds = jitter(value) if jitter is not None else value
except TypeError:
warnings.warn(
"Nullary jitter function signature is deprecated. Use "
"unary signature accepting a wait value in seconds and "
"returning a jittered version of it.",
DeprecationWarning,
stacklevel=2,
)

seconds = value + jitter() # type: ignore[call-arg, misc] # ty:ignore[missing-argument]
seconds = jitter(value) if jitter is not None else value

# don't sleep longer than remaining allotted max_time
if max_time is not None:
Expand Down
22 changes: 6 additions & 16 deletions tests/test_backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,7 @@ def emptiness(*args, **kwargs):
}


# To maintain backward compatibility,
# on_predicate should support 0-argument jitter function.
def test_on_exception_success_0_arg_jitter(monkeypatch: pytest.MonkeyPatch) -> None:
def test_on_exception_jitter(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("time.sleep", lambda x: None)

backoffs: list[Details] = []
Expand All @@ -555,7 +553,7 @@ def test_on_exception_success_0_arg_jitter(monkeypatch: pytest.MonkeyPatch) -> N
on_success=successes.append,
on_backoff=backoffs.append,
on_giveup=giveups.append,
jitter=lambda: 0.0, # type:ignore[arg-type,misc] # ty:ignore[invalid-argument-type]
jitter=lambda value: 0.0,
interval=0,
)
@_save_target
Expand All @@ -564,10 +562,7 @@ def succeeder(*args, **kwargs):
if len(backoffs) < 2:
raise ValueError("catch me")

with pytest.deprecated_call(
match="Nullary jitter function signature is deprecated",
):
succeeder(1, 2, 3, foo=1, bar=2)
succeeder(1, 2, 3, foo=1, bar=2)

# we try 3 times, backing off twice before succeeding
assert len(successes) == 1
Expand Down Expand Up @@ -596,9 +591,7 @@ def succeeder(*args, **kwargs):
}


# To maintain backward compatibility,
# on_predicate should support 0-argument jitter function.
def test_on_predicate_success_0_arg_jitter(monkeypatch: pytest.MonkeyPatch) -> None:
def test_on_predicate_jitter(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("time.sleep", lambda x: None)

backoffs: list[Details] = []
Expand All @@ -610,18 +603,15 @@ def test_on_predicate_success_0_arg_jitter(monkeypatch: pytest.MonkeyPatch) -> N
on_success=successes.append,
on_backoff=backoffs.append,
on_giveup=giveups.append,
jitter=lambda: 0.0, # type:ignore[arg-type,misc] # ty:ignore[invalid-argument-type]
jitter=lambda value: 0.0,
interval=0,
)
@_save_target
def success(*args, **kwargs):
# succeed after we've backed off twice
return len(backoffs) == 2

with pytest.deprecated_call(
match="Nullary jitter function signature is deprecated",
):
success(1, 2, 3, foo=1, bar=2)
success(1, 2, 3, foo=1, bar=2)

# we try 3 times, backing off twice before succeeding
assert len(successes) == 1
Expand Down
26 changes: 6 additions & 20 deletions tests/test_backoff_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,8 @@ async def falsey():
assert len(successes) == 0


# To maintain backward compatibility,
# on_predicate should support 0-argument jitter function.
@pytest.mark.asyncio
async def test_on_exception_success_0_arg_jitter(
monkeypatch: pytest.MonkeyPatch,
) -> None:
async def test_on_exception_jitter(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("asyncio.sleep", _await_none)

log, log_success, log_backoff, log_giveup = _log_hdlrs()
Expand All @@ -535,7 +531,7 @@ async def test_on_exception_success_0_arg_jitter(
on_success=log_success,
on_backoff=log_backoff,
on_giveup=log_giveup,
jitter=lambda: 0.0, # type:ignore[arg-type,misc] # ty:ignore[invalid-argument-type]
jitter=lambda value: 0.0,
interval=0,
)
@_save_target
Expand All @@ -544,10 +540,7 @@ async def succeeder(*args, **kwargs):
if len(log["backoff"]) < 2:
raise ValueError("catch me")

with pytest.deprecated_call(
match="Nullary jitter function signature is deprecated",
):
await succeeder(1, 2, 3, foo=1, bar=2)
await succeeder(1, 2, 3, foo=1, bar=2)

# we try 3 times, backing off twice before succeeding
assert len(log["success"]) == 1
Expand Down Expand Up @@ -576,12 +569,8 @@ async def succeeder(*args, **kwargs):
}


# To maintain backward compatibility,
# on_predicate should support 0-argument jitter function.
@pytest.mark.asyncio
async def test_on_predicate_success_0_arg_jitter(
monkeypatch: pytest.MonkeyPatch,
) -> None:
async def test_on_predicate_jitter(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("asyncio.sleep", _await_none)

log, log_success, log_backoff, log_giveup = _log_hdlrs()
Expand All @@ -591,18 +580,15 @@ async def test_on_predicate_success_0_arg_jitter(
on_success=log_success,
on_backoff=log_backoff,
on_giveup=log_giveup,
jitter=lambda: 0.0, # type:ignore[arg-type,misc] # ty:ignore[invalid-argument-type]
jitter=lambda value: 0.0,
interval=0,
)
@_save_target
async def success(*args, **kwargs):
# succeed after we've backed off twice
return len(log["backoff"]) == 2

with pytest.deprecated_call(
match="Nullary jitter function signature is deprecated",
):
await success(1, 2, 3, foo=1, bar=2)
await success(1, 2, 3, foo=1, bar=2)

# we try 3 times, backing off twice before succeeding
assert len(log["success"]) == 1
Expand Down
Loading