Accept empty message in retry_if_exception_message#651
Open
gaoflow wants to merge 1 commit into
Open
Conversation
retry_if_exception_message validated its message/match arguments with
truthiness checks (if message and match, if not message and not match).
An empty string is a valid message target — it matches exceptions whose
str() is empty, e.g. a bare RuntimeError() — but 'not message' is True
for '', so retry_if_exception_message(message='') wrongly raised
TypeError('missing 1 required argument'). _check had the same problem:
'if self.message:' skipped the empty-message branch.
Replace the truthiness guards with 'is None' checks so an empty message
is accepted and matched. This makes every message matchable, not every
message except the empty one.
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.
Problem
retry_if_exception_messagevalidates its two mutually-exclusive argumentswith truthiness checks:
An empty string is a valid message target — it matches exceptions whose
str()is empty, e.g. a bareraise RuntimeError()(str(RuntimeError()) == "").But
not messageisTruefor"", so:The result is an asymmetry: you can match every exception message except
the empty one.
_check'sif self.message:would drop the empty-message branchas well, falling through to
assert self.match is not None.Fix
Replace the truthiness guards with
is Nonechecks, so an empty message isaccepted and matched like any other.
retry_if_not_exception_messageinheritsthe same constructor and is fixed by the same change.
Tests
Added
test_retry_if_exception_message_empty_messageandtest_retry_if_not_exception_message_empty_message: construct withmessage=""and drive the predicate against a bare
RuntimeError()(matches) and aRuntimeError("boom")(does not), plus the inverse for the not-variant. Bothfail before the change (
TypeErrorat construction) and pass after. Theexisting
test_..._negative_no_inputs(passing no arguments must raise) stillpasses.
Full suite:
167 passed, 1 skipped, 12 subtests.ruff check/ruff format --checkclean.