Skip to content

Commit 11f7615

Browse files
max-muotogerzse
authored andcommitted
Add missing type hints for backoff.py (redis#3249)
Add type hints to backoff.py
1 parent 3948063 commit 11f7615

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

redis/backoff.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -19,79 +19,79 @@ def reset(self):
1919
pass
2020

2121
@abstractmethod
22-
def compute(self, failures):
22+
def compute(self, failures: int) -> float:
2323
"""Compute backoff in seconds upon failure"""
2424
pass
2525

2626

2727
class ConstantBackoff(AbstractBackoff):
2828
"""Constant backoff upon failure"""
2929

30-
def __init__(self, backoff):
30+
def __init__(self, backoff: float) -> None:
3131
"""`backoff`: backoff time in seconds"""
3232
self._backoff = backoff
3333

34-
def compute(self, failures):
34+
def compute(self, failures: int) -> float:
3535
return self._backoff
3636

3737

3838
class NoBackoff(ConstantBackoff):
3939
"""No backoff upon failure"""
4040

41-
def __init__(self):
41+
def __init__(self) -> None:
4242
super().__init__(0)
4343

4444

4545
class ExponentialBackoff(AbstractBackoff):
4646
"""Exponential backoff upon failure"""
4747

48-
def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
48+
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE):
4949
"""
5050
`cap`: maximum backoff time in seconds
5151
`base`: base backoff time in seconds
5252
"""
5353
self._cap = cap
5454
self._base = base
5555

56-
def compute(self, failures):
56+
def compute(self, failures: int) -> float:
5757
return min(self._cap, self._base * 2**failures)
5858

5959

6060
class FullJitterBackoff(AbstractBackoff):
6161
"""Full jitter backoff upon failure"""
6262

63-
def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
63+
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
6464
"""
6565
`cap`: maximum backoff time in seconds
6666
`base`: base backoff time in seconds
6767
"""
6868
self._cap = cap
6969
self._base = base
7070

71-
def compute(self, failures):
71+
def compute(self, failures: int) -> float:
7272
return random.uniform(0, min(self._cap, self._base * 2**failures))
7373

7474

7575
class EqualJitterBackoff(AbstractBackoff):
7676
"""Equal jitter backoff upon failure"""
7777

78-
def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
78+
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
7979
"""
8080
`cap`: maximum backoff time in seconds
8181
`base`: base backoff time in seconds
8282
"""
8383
self._cap = cap
8484
self._base = base
8585

86-
def compute(self, failures):
86+
def compute(self, failures: int) -> float:
8787
temp = min(self._cap, self._base * 2**failures) / 2
8888
return temp + random.uniform(0, temp)
8989

9090

9191
class DecorrelatedJitterBackoff(AbstractBackoff):
9292
"""Decorrelated jitter backoff upon failure"""
9393

94-
def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
94+
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
9595
"""
9696
`cap`: maximum backoff time in seconds
9797
`base`: base backoff time in seconds
@@ -100,10 +100,10 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
100100
self._base = base
101101
self._previous_backoff = 0
102102

103-
def reset(self):
103+
def reset(self) -> None:
104104
self._previous_backoff = 0
105105

106-
def compute(self, failures):
106+
def compute(self, failures: int) -> float:
107107
max_backoff = max(self._base, self._previous_backoff * 3)
108108
temp = random.uniform(self._base, max_backoff)
109109
self._previous_backoff = min(self._cap, temp)

0 commit comments

Comments
 (0)