diff --git a/pyproject.toml b/pyproject.toml index 3f5d511..27899e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] readme = "README.md" diff --git a/pyretries/retry.py b/pyretries/retry.py index f622599..8624bda 100644 --- a/pyretries/retry.py +++ b/pyretries/retry.py @@ -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: diff --git a/pyretries/strategy.py b/pyretries/strategy.py index 8a082f8..3bfd7c0 100644 --- a/pyretries/strategy.py +++ b/pyretries/strategy.py @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/tests/test_strategy.py b/tests/test_strategy.py index f1b09f9..ce2d81d 100644 --- a/tests/test_strategy.py +++ b/tests/test_strategy.py @@ -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 @@ -29,7 +29,7 @@ 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) @@ -37,7 +37,7 @@ 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 @@ -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 @@ -85,7 +85,7 @@ 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) @@ -93,7 +93,7 @@ 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 @@ -106,7 +106,7 @@ 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 @@ -114,7 +114,7 @@ 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) @@ -122,7 +122,7 @@ 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 @@ -133,7 +133,7 @@ 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): @@ -141,7 +141,7 @@ def test_raises_when_max_attempts(self): with pytest.raises(RetryStrategyExausted) as err: for _ in range(3): - s.maybe_apply(1) + s.eval(1) assert err.value.__cause__ == None @@ -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