Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing type hints for retry.py #3250

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading