Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid confusion between strategy and retry #10

Merged
merged 2 commits into from
Oct 13, 2023
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: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyretries"
version = "0.2.7"
version = "0.2.8"
description = "A Python retry package based on strategies"
authors = ["Ben Mezger <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion pyretries/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def exec_strategy(self, state: RetryState[ReturnT]):
f"Current attempt {state.current_attempts}"
)

state.strategy.maybe_apply(state.returned_value)
state.strategy.eval(state.returned_value)
state.current_attempts += 1

if state.strategy.should_stop:
Expand Down
24 changes: 12 additions & 12 deletions pyretries/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def should_stop(self) -> bool:
raise NotImplementedError

@abc.abstractmethod
def maybe_apply(self, value: StrategyValueT | Exception | None) -> bool:
def eval(self, value: StrategyValueT | Exception | None) -> bool:
"""
Checks if strategy should be applied.
Evaluates strategy.

Args:
value: `func` Returned value or exception.
Expand Down Expand Up @@ -81,9 +81,9 @@ def should_stop(self) -> bool:
return True
return False

def maybe_apply(self, value: StrategyValueT | Exception | None) -> bool:
def eval(self, value: StrategyValueT | Exception | None) -> bool:
"""
Check if strategy should apply.
Evaluates strategy.

Args:
value: `func` Returned value or exception.
Expand Down Expand Up @@ -132,9 +132,9 @@ def should_stop(self) -> bool:
return True
return False

def maybe_apply(self, value: StrategyValueT | Exception | None) -> bool:
def eval(self, value: StrategyValueT | Exception | None) -> bool:
"""
Check if strategy should apply.
Evaluates strategy.

Args:
value: `func` Returned value or exception.
Expand Down Expand Up @@ -186,9 +186,9 @@ def should_stop(self) -> bool:
return True
return False

def maybe_apply(self, value: StrategyValueT | Exception | None) -> bool:
def eval(self, value: StrategyValueT | Exception | None) -> bool:
"""
Check if strategy should apply.
Evaluates strategy.

Args:
value: `func` Returned value or exception.
Expand Down Expand Up @@ -237,9 +237,9 @@ def should_stop(self) -> bool:
return True
return False

def maybe_apply(self, value: StrategyValueT | Exception | None) -> bool:
def eval(self, value: StrategyValueT | Exception | None) -> bool:
"""
Check if strategy should apply.
Evaluates strategy.

Args:
value: `func` Returned value or exception.
Expand Down Expand Up @@ -292,9 +292,9 @@ def should_stop(self) -> bool:
return True
return False

def maybe_apply(self, value: StrategyValueT | Exception | None) -> bool:
def eval(self, value: StrategyValueT | Exception | None) -> bool:
"""
Check if strategy should apply.
Evaluates strategy.

Args:
value: `func` Returned value or exception.
Expand Down
24 changes: 12 additions & 12 deletions tests/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_sleeps_for_n_seconds(self, seconds: int, attempts: int, sleep: MagicMoc

with pytest.raises(RetryStrategyExausted):
for _ in range(attempts + 1):
s.maybe_apply(None)
s.eval(None)

sleep.assert_called_with(seconds)
assert s.current_attempt == attempts
Expand All @@ -29,15 +29,15 @@ def test_raises_from_value(self):
s = strategy.SleepStrategy(1, 0)

with pytest.raises(RetryStrategyExausted) as err:
s.maybe_apply(ValueError("Error"))
s.eval(ValueError("Error"))

assert isinstance(err.value.__cause__, ValueError)

def test_raises_from_none_when_value_is_not_an_exception(self):
s = strategy.SleepStrategy(1, 0)

with pytest.raises(RetryStrategyExausted) as err:
s.maybe_apply(None)
s.eval(None)

assert err.value.__cause__ == None

Expand Down Expand Up @@ -75,7 +75,7 @@ def test_sleeps_for_n_delay(

with pytest.raises(RetryStrategyExausted):
for _ in range(attempts + 1):
s.maybe_apply(None)
s.eval(None)

# checks if sleep was called N types with the calculated delay
assert [call.args[0] for call in sleep.call_args_list] == expected_delays
Expand All @@ -85,15 +85,15 @@ def test_raises_from_value(self):
s = strategy.ExponentialBackoffStrategy(0, 1)

with pytest.raises(RetryStrategyExausted) as err:
s.maybe_apply(ValueError("Error"))
s.eval(ValueError("Error"))

assert isinstance(err.value.__cause__, ValueError)

def test_raises_from_none_when_value_is_not_an_exception(self):
s = strategy.ExponentialBackoffStrategy(0, 0)

with pytest.raises(RetryStrategyExausted) as err:
s.maybe_apply(None)
s.eval(None)

assert err.value.__cause__ == None

Expand All @@ -106,23 +106,23 @@ def test_sleeps_for_n_seconds(self, attempts: int):

with pytest.raises(RetryStrategyExausted):
for _ in range(attempts + 1):
s.maybe_apply(None)
s.eval(None)

assert s.current_attempt == attempts

def test_raises_from_value(self):
s = strategy.StopAfterAttemptStrategy(0)

with pytest.raises(RetryStrategyExausted) as err:
s.maybe_apply(ValueError("Error"))
s.eval(ValueError("Error"))

assert isinstance(err.value.__cause__, ValueError)

def test_raises_from_none_when_value_is_not_an_exception(self):
s = strategy.StopAfterAttemptStrategy(0)

with pytest.raises(RetryStrategyExausted) as err:
s.maybe_apply(None)
s.eval(None)

assert err.value.__cause__ == None

Expand All @@ -133,15 +133,15 @@ def test_return_value(self, attempts: int):
s = strategy.StopWhenReturnValueStrategy(expected=attempts, max_attempts=2)
assert s.current_attempt == 0

assert s.maybe_apply(attempts) is False
assert s.eval(attempts) is False
assert s.current_attempt == 1

def test_raises_when_max_attempts(self):
s = strategy.StopWhenReturnValueStrategy(None, 2)

with pytest.raises(RetryStrategyExausted) as err:
for _ in range(3):
s.maybe_apply(1)
s.eval(1)

assert err.value.__cause__ == None

Expand All @@ -154,7 +154,7 @@ def test_return_value(self, attempts: int):

with pytest.raises(RetryStrategyExausted) as err:
for _ in range(attempts + 1):
s.maybe_apply(1)
s.eval(1)

assert s.current_attempt == attempts
assert err.value.__cause__ == None