Skip to content

Commit 09e8b16

Browse files
committed
Make ExponentialBackoff and LinearBackoff require keyword arguments
The `ExponentialBackoff` and `LinearBackoff` classes now require keyword arguments for their constructor. This change was made to make the classes easier to use and to avoid confusion with the order of the arguments. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent e3ef0fb commit 09e8b16

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- `GrpcStreamBroadcaster` now takes a `AsyncIterable` instead of a `AsyncIterator` as the `stream_method`. This is to match the type of streaming methods generated by `grpc`, so no conversion to an `AsyncIterator` is needed.
1010
- gRPC URLs don't have a default port anymore, unless a default is set via `ChannelOptions`. If you want to set a default port for URLs, please pass custom `ChannelOptions` as `defaults` to `parse_grpc_uri` or as `channel_defaults` to `BaseApiClient`.
11+
* The `ExponentialBackoff` and `LinearBackoff` classes now require keyword arguments for their constructor. This change was made to make the classes easier to use and to avoid confusion with the order of the arguments.
1112

1213
## New Features
1314

src/frequenz/client/base/retry.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class LinearBackoff(Strategy):
7979

8080
def __init__(
8181
self,
82+
*,
8283
interval: float = DEFAULT_RETRY_INTERVAL,
8384
jitter: float = DEFAULT_RETRY_JITTER,
8485
limit: int | None = None,
@@ -124,9 +125,9 @@ class ExponentialBackoff(Strategy):
124125
DEFAULT_MULTIPLIER = 2.0
125126
"""Default multiplier for exponential increment."""
126127

127-
# pylint: disable=too-many-arguments
128-
def __init__(
128+
def __init__( # pylint: disable=too-many-arguments
129129
self,
130+
*,
130131
initial_interval: float = DEFAULT_INTERVAL,
131132
max_interval: float = DEFAULT_MAX_INTERVAL,
132133
multiplier: float = DEFAULT_MULTIPLIER,

tests/retry_strategy/test_retry_strategy.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_no_limit(self) -> None:
2323

2424
def test_iter(self) -> None:
2525
"""Test iterator."""
26-
assert list(retry.LinearBackoff(1, 0, 3)) == [1, 1, 1]
26+
assert list(retry.LinearBackoff(interval=1, jitter=0, limit=3)) == [1, 1, 1]
2727

2828
def test_with_limit(self) -> None:
2929
"""Test limit works."""
@@ -89,7 +89,7 @@ def test_with_jitter_with_limit(self) -> None:
8989

9090
def test_deep_copy(self) -> None:
9191
"""Test if deep copies are really deep copies."""
92-
strategy = retry.LinearBackoff(1.0, 0.0, 2)
92+
strategy = retry.LinearBackoff(interval=1.0, jitter=0.0, limit=2)
9393

9494
copy1 = strategy.copy()
9595
assert copy1.next_interval() == 1.0
@@ -108,7 +108,9 @@ class TestExponentialBackoff:
108108

109109
def test_no_limit(self) -> None:
110110
"""Test base case."""
111-
strategy = retry.ExponentialBackoff(3, 30, 2, 0.0)
111+
strategy = retry.ExponentialBackoff(
112+
initial_interval=3, max_interval=30, multiplier=2, jitter=0.0
113+
)
112114

113115
assert strategy.next_interval() == 3.0
114116
assert strategy.next_interval() == 6.0
@@ -119,7 +121,7 @@ def test_no_limit(self) -> None:
119121

120122
def test_with_limit(self) -> None:
121123
"""Test limit works."""
122-
strategy = retry.ExponentialBackoff(3, jitter=0.0, limit=3)
124+
strategy = retry.ExponentialBackoff(initial_interval=3, jitter=0.0, limit=3)
123125

124126
assert strategy.next_interval() == 3.0
125127
assert strategy.next_interval() == 6.0
@@ -128,7 +130,9 @@ def test_with_limit(self) -> None:
128130

129131
def test_deep_copy(self) -> None:
130132
"""Test if deep copies are really deep copies."""
131-
strategy = retry.ExponentialBackoff(3.0, 30.0, 2, 0.0, 2)
133+
strategy = retry.ExponentialBackoff(
134+
initial_interval=3.0, max_interval=30.0, multiplier=2, jitter=0.0, limit=2
135+
)
132136

133137
copy1 = strategy.copy()
134138
assert copy1.next_interval() == 3.0

0 commit comments

Comments
 (0)