Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

terencehonles
Copy link
Contributor

@terencehonles terencehonles commented Jun 3, 2025

Description of change

This change fixes a typo in ExponentialWithJitterBackoff's __eq__ method from #3628, and allows async Retry objects to also support __eq__ and __hash__.

Pull Request check-list

Please make sure to review and check all of these items:

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.

This change fixes a typo in `ExponentialWithJitterBackoff`'s `__eq__` method.
@terencehonles terencehonles changed the title fix ExponentialWithJitterBackoff __eq__ add async Retry __eq__ and __hash__ & fix ExponentialWithJitterBackoff __eq__ Jun 3, 2025
@terencehonles terencehonles force-pushed the patch-1 branch 5 times, most recently from b32dc30 to 0278734 Compare June 3, 2025 15:04
"""Retry a specific number of times after a failure"""

__slots__ = "_backoff", "_retries", "_supported_errors"
Copy link
Collaborator

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

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):
Copy link
Collaborator

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.

@petyaslavova petyaslavova requested a review from Copilot June 9, 2025 08:12
Copy link
Contributor

@Copilot Copilot AI left a 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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants