Skip to content

Commit 511fda0

Browse files
max-muotogerzse
authored andcommitted
Add missing type hints for retry.py (#3250)
Add missing type hints in the retry.py file and related tests.
1 parent c1a6ff7 commit 511fda0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

redis/retry.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import socket
22
from time import sleep
3+
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar
34

45
from redis.exceptions import ConnectionError, TimeoutError
56

7+
T = TypeVar("T")
8+
9+
if TYPE_CHECKING:
10+
from redis.backoff import AbstractBackoff
11+
612

713
class Retry:
814
"""Retry a specific number of times after a failure"""
915

1016
def __init__(
1117
self,
12-
backoff,
13-
retries,
14-
supported_errors=(ConnectionError, TimeoutError, socket.timeout),
18+
backoff: "AbstractBackoff",
19+
retries: int,
20+
supported_errors: Tuple[Type[Exception], ...] = (
21+
ConnectionError,
22+
TimeoutError,
23+
socket.timeout,
24+
),
1525
):
1626
"""
1727
Initialize a `Retry` object with a `Backoff` object
@@ -24,15 +34,21 @@ def __init__(
2434
self._retries = retries
2535
self._supported_errors = supported_errors
2636

27-
def update_supported_errors(self, specified_errors: list):
37+
def update_supported_errors(
38+
self, specified_errors: Iterable[Type[Exception]]
39+
) -> None:
2840
"""
2941
Updates the supported errors with the specified error types
3042
"""
3143
self._supported_errors = tuple(
3244
set(self._supported_errors + tuple(specified_errors))
3345
)
3446

35-
def call_with_retry(self, do, fail):
47+
def call_with_retry(
48+
self,
49+
do: Callable[[], T],
50+
fail: Callable[[Exception], Any],
51+
) -> T:
3652
"""
3753
Execute an operation that might fail and returns its result, or
3854
raise the exception that was thrown depending on the `Backoff` object.

tests/test_retry.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import patch
22

33
import pytest
4-
from redis.backoff import ExponentialBackoff, NoBackoff
4+
from redis.backoff import AbstractBackoff, ExponentialBackoff, NoBackoff
55
from redis.client import Redis
66
from redis.connection import Connection, UnixDomainSocketConnection
77
from redis.exceptions import (
@@ -15,7 +15,7 @@
1515
from .conftest import _get_client
1616

1717

18-
class BackoffMock:
18+
class BackoffMock(AbstractBackoff):
1919
def __init__(self):
2020
self.reset_calls = 0
2121
self.calls = 0

0 commit comments

Comments
 (0)