-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 1 commit
b0bdb76
1660336
8df463b
663bc61
84dc933
b0d4196
05f404a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
retries: int, | ||
supported_errors: tuple[type[Exception], ...] = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same about supporting Python 3.8. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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 usingif TYPE_CHECKING
?Tbh, it feels cleaner to just import
AbstractBackoff
always, it should not be a performance penalty.There was a problem hiding this comment.
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.