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
Changes from 1 commit
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, Iterable, TypeVar, Callable

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should AbstractBackoff be quoted here, when using if TYPE_CHECKING?

Tbh, it feels cleaner to just import AbstractBackoff always, it should not be a performance penalty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, went ahead and quoted it: 1660336

It would be nicer to quote, just to avoid an unnecessary import which would slow down module init time.

retries: int,
supported_errors: tuple[type[Exception], ...] = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support Python 3.8, where this is not yet available. You can use Tuple[Type[Exception], ...] and import them from typing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, good catch!

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]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about supporting Python 3.8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

) -> 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