From ab66dfaf5ba1777bd66687c06f17d79e6e5cb620 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Mon, 27 May 2024 15:30:05 -0700 Subject: [PATCH] Add missing type hints for backoff.py --- redis/backoff.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/redis/backoff.py b/redis/backoff.py index c62e760bdc..f612d60704 100644 --- a/redis/backoff.py +++ b/redis/backoff.py @@ -19,7 +19,7 @@ def reset(self): pass @abstractmethod - def compute(self, failures): + def compute(self, failures: int) -> float: """Compute backoff in seconds upon failure""" pass @@ -27,25 +27,25 @@ def compute(self, failures): class ConstantBackoff(AbstractBackoff): """Constant backoff upon failure""" - def __init__(self, backoff): + def __init__(self, backoff: float) -> None: """`backoff`: backoff time in seconds""" self._backoff = backoff - def compute(self, failures): + def compute(self, failures: int) -> float: return self._backoff class NoBackoff(ConstantBackoff): """No backoff upon failure""" - def __init__(self): + def __init__(self) -> None: super().__init__(0) class ExponentialBackoff(AbstractBackoff): """Exponential backoff upon failure""" - def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): + def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE): """ `cap`: maximum backoff time in seconds `base`: base backoff time in seconds @@ -53,14 +53,14 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): self._cap = cap self._base = base - def compute(self, failures): + def compute(self, failures: int) -> float: return min(self._cap, self._base * 2**failures) class FullJitterBackoff(AbstractBackoff): """Full jitter backoff upon failure""" - def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): + def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None: """ `cap`: maximum backoff time in seconds `base`: base backoff time in seconds @@ -68,14 +68,14 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): self._cap = cap self._base = base - def compute(self, failures): + def compute(self, failures: int) -> float: return random.uniform(0, min(self._cap, self._base * 2**failures)) class EqualJitterBackoff(AbstractBackoff): """Equal jitter backoff upon failure""" - def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): + def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None: """ `cap`: maximum backoff time in seconds `base`: base backoff time in seconds @@ -83,7 +83,7 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): self._cap = cap self._base = base - def compute(self, failures): + def compute(self, failures: int) -> float: temp = min(self._cap, self._base * 2**failures) / 2 return temp + random.uniform(0, temp) @@ -91,7 +91,7 @@ def compute(self, failures): class DecorrelatedJitterBackoff(AbstractBackoff): """Decorrelated jitter backoff upon failure""" - def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): + def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None: """ `cap`: maximum backoff time in seconds `base`: base backoff time in seconds @@ -100,10 +100,10 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE): self._base = base self._previous_backoff = 0 - def reset(self): + def reset(self) -> None: self._previous_backoff = 0 - def compute(self, failures): + def compute(self, failures: int) -> float: max_backoff = max(self._base, self._previous_backoff * 3) temp = random.uniform(self._base, max_backoff) self._previous_backoff = min(self._cap, temp)