Fix retry_if_exception_message rejecting empty string arguments#598
Open
Fix retry_if_exception_message rejecting empty string arguments#598
Conversation
The truthiness checks (if message, elif match) treat empty string as falsy, so message='' or match='' falls through to the else branch and raises TypeError. These are valid values — an empty message matches exceptions like Exception() where str(exc) == ''. Switched to 'is not None' checks.
jd
requested changes
Feb 23, 2026
Owner
jd
left a comment
There was a problem hiding this comment.
The fix is correct — nice catch on the truthiness vs is not None distinction. All three conditionals are fixed consistently, and the mutual exclusion check now correctly catches message="", match="foo".
However, please add test cases to prevent regression. At minimum:
def test_retry_if_exception_message_empty_string(self):
r = retry_if_exception_message(message="")
# Should match Exception() since str(Exception()) == ""
self.assertTrue(r(make_retry_state(Exception())))
# Should not match Exception("foo")
self.assertFalse(r(make_retry_state(Exception("foo"))))
def test_retry_if_exception_message_match_empty_string(self):
r = retry_if_exception_message(match="")
# Empty regex matches everything
self.assertTrue(r(make_retry_state(Exception("anything"))))
def test_retry_if_exception_message_both_empty(self):
with self.assertRaises(TypeError):
retry_if_exception_message(message="", match="")Same cases should be added for retry_if_not_exception_message since it inherits the constructor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
retry_if_exception_message(message="")incorrectly raisesTypeErrorinstead of creating a predicate that matches exceptions with empty string representations.The Bug
The constructor uses Python truthiness checks (
if message,elif match) instead ofis not Nonechecks. Since empty string""is falsy:message=""falls through toelseand raisesTypeErrormatch=""falls through toelseand raisesTypeErrormessage="", match="foo"silently usesmatchinstead of raising the mutual exclusion errorThe same issue affects
retry_if_not_exception_messagesince it inherits the constructor.The Fix
Replaced
if message/elif matchwithif message is not None/elif match is not None.