Skip to content

Feat: add retry_unless_exception_cause_type (#625)#626

Open
Kitan-Dara06 wants to merge 6 commits intojd:mainfrom
Kitan-Dara06:feat-retry-unless-cause
Open

Feat: add retry_unless_exception_cause_type (#625)#626
Kitan-Dara06 wants to merge 6 commits intojd:mainfrom
Kitan-Dara06:feat-retry-unless-cause

Conversation

@Kitan-Dara06
Copy link

This PR introduces the retry_unless_exception_cause_type class to mirror the existing retry_unless_exception_type logic. It halts the retry loop immediately if the root cause of the raised exception matches the specified type.

Related Issue:
Resolves #625

Testing:

[x] Implemented core logic in tenacity/retry.py

[x] Added unit tests in tests/test_tenacity.py confirming the retry loop halts on a matched cause and continues otherwise.

[x] Passed all local pytest checks (149/149).

@rgullik1
Copy link

rgullik1 commented Mar 9, 2026

The new class LGTM, but I don't really think the inverse overload behaves as one would expect to make it useful.
Technically it does invert but it will retry when there's no exceptions. I don't really think that's useful. Would be better to just add the new class IMO.

Comment on lines +60 to +63
def __invert__(self) -> "retry_base":
"""Return a retry strategy that is the logical inverse of this one."""
return _retry_inverted(self)

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

I don't think the invert overload will be useful, please remove

Comment on lines +68 to +81
class _retry_inverted(retry_base):
"""Retry strategy that inverts the decision of another retry strategy."""

def __init__(self, retry: "retry_base") -> None:
self.retry = retry

def __call__(self, retry_state: "RetryCallState") -> bool:
return not self.retry(retry_state)

def __invert__(self) -> "retry_base":
# Double inversion returns the original.
return self.retry


Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Remove this also.

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.

[FEATURE REQUEST] retry_unless_exception_cause_type

2 participants