-
Notifications
You must be signed in to change notification settings - Fork 2.6k
add async Retry __eq__
and __hash__
& fix ExponentialWithJitterBackoff __eq__
#3668
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
base: master
Are you sure you want to change the base?
Conversation
This change fixes a typo in `ExponentialWithJitterBackoff`'s `__eq__` method.
__eq__
__eq__
and __hash__
& fix ExponentialWithJitterBackoff __eq__
b32dc30
to
0278734
Compare
"""Retry a specific number of times after a failure""" | ||
|
||
__slots__ = "_backoff", "_retries", "_supported_errors" |
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.
I think that we don't need to use __slots__
for the retry
object. We are not keeping that many instances of it, and the saved memory is not going to be significant. I see that we've had both patterns in the sync
and async
versions, and I suggest sticking to the one that has been used by the sync Retry
class.
TimeoutError, | ||
socket.timeout, | ||
), | ||
supported_errors: Union[Tuple[Type[Exception], ...], None] = None, |
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.
I think it would be better here if you extract the list as a constant and assign this list as the default value - it will be more visible what will be assigned to the list in case the user doesn't provide a value.
|
||
def __eq__(self, other: Any) -> bool: | ||
if not isinstance(other, Retry): | ||
if not isinstance(other, AbstractRetry): |
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.
I think we should have __eq__
implemented in each class(sync and async) and check if it is an instance of the concrete classes.
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.
Pull Request Overview
This PR fixes the equality comparison in ExponentialWithJitterBackoff and extends support for eq and hash to async Retry objects. It also refactors the Retry code into an abstract base class and updates the async retry implementation accordingly.
- Updated tests to parameterize both sync and async Retry objects.
- Refactored the Retry class into AbstractRetry with a subclass, and corrected the equality check for ExponentialWithJitterBackoff.
- Modified the async Retry class to inherit from AbstractRetry and revised the type hint for the fail callback.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
File | Description |
---|---|
tests/test_retry.py | Updated tests to cover both sync and async Retry equality and hash. |
redis/retry.py | Refactored Retry into AbstractRetry and fixed supported_errors handling in eq. |
redis/backoff.py | Corrected the equality check to validate against the appropriate class. |
redis/asyncio/retry.py | Changed Retry to subclass AbstractRetry and adjusted the fail callback type. |
Comments suppressed due to low confidence (2)
redis/retry.py:34
- If supported_errors is not provided, _supported_errors is never set, which could lead to an AttributeError during equality checks. Consider initializing _supported_errors to a default value (e.g., an empty tuple) when supported_errors is None.
if supported_errors:
redis/asyncio/retry.py:17
- Changing the type for the fail callback from RedisError to Exception broadens the error types handled. Confirm that this change is intentional to avoid inadvertently catching non-Redis errors.
self, do: Callable[[], Awaitable[T]], fail: Callable[[Exception], Any]
Description of change
This change fixes a typo in
ExponentialWithJitterBackoff
's__eq__
method from #3628, and allows asyncRetry
objects to also support__eq__
and__hash__
.Pull Request check-list
Please make sure to review and check all of these items:
NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.