Skip to content

Commit

Permalink
Add missing type hints for retry.py (#3250)
Browse files Browse the repository at this point in the history
Add missing type hints in the retry.py file and related tests.
  • Loading branch information
max-muoto authored and gerzse committed Jul 11, 2024
1 parent c1a6ff7 commit 511fda0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 21 additions & 5 deletions redis/retry.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import socket
from time import sleep
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar

from redis.exceptions import ConnectionError, TimeoutError

T = TypeVar("T")

if TYPE_CHECKING:
from redis.backoff import AbstractBackoff


class Retry:
"""Retry a specific number of times after a failure"""

def __init__(
self,
backoff,
retries,
supported_errors=(ConnectionError, TimeoutError, socket.timeout),
backoff: "AbstractBackoff",
retries: int,
supported_errors: Tuple[Type[Exception], ...] = (
ConnectionError,
TimeoutError,
socket.timeout,
),
):
"""
Initialize a `Retry` object with a `Backoff` object
Expand All @@ -24,15 +34,21 @@ def __init__(
self._retries = retries
self._supported_errors = supported_errors

def update_supported_errors(self, specified_errors: list):
def update_supported_errors(
self, specified_errors: Iterable[Type[Exception]]
) -> None:
"""
Updates the supported errors with the specified error types
"""
self._supported_errors = tuple(
set(self._supported_errors + tuple(specified_errors))
)

def call_with_retry(self, do, fail):
def call_with_retry(
self,
do: Callable[[], T],
fail: Callable[[Exception], Any],
) -> T:
"""
Execute an operation that might fail and returns its result, or
raise the exception that was thrown depending on the `Backoff` object.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_retry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import patch

import pytest
from redis.backoff import ExponentialBackoff, NoBackoff
from redis.backoff import AbstractBackoff, ExponentialBackoff, NoBackoff
from redis.client import Redis
from redis.connection import Connection, UnixDomainSocketConnection
from redis.exceptions import (
Expand All @@ -15,7 +15,7 @@
from .conftest import _get_client


class BackoffMock:
class BackoffMock(AbstractBackoff):
def __init__(self):
self.reset_calls = 0
self.calls = 0
Expand Down

0 comments on commit 511fda0

Please sign in to comment.