retry_if_exception_message: use re.search for the match= pattern so it finds the regex anywhere in the message#652
Open
HrachShah wants to merge 1 commit into
Conversation
…t finds the regex anywhere in the exception message, not only at the start
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.
Bug
retry_if_exception_message(match=...)compiled the user's regex and calledre.Pattern.match(), which only matches at the start of the string. The parameter is namedmatchand the docstring just says "matches" — so users reasonably expect the pattern to be found anywhere in the exception message, the same wayre.searchdoes. With the current implementation, a user who wrote:got a retry on
ValueError("rate limit hit")(works by accident) but no retry onValueError("HTTP 429: rate limit hit")(pattern not at the start). That second case is the common real-world one and is exactly the kind of message HTTP libraries produce.Fix
Use
self.match.search(...)instead ofself.match.match(...)inretry_if_exception_message._check. The behaviour ofmessage=(full-string equality) is unchanged.Test
Added
test_retry_if_exception_message_match_finds_pattern_anywhereplus a module-level helper that raisesCustomError("HTTP 429: rate limit exceeded")and matches on"limit exceeded"(which does not occur at position 0). The pre-existing testtest_retry_if_exception_message_matchstill passes — it uses a pattern prefixed withderived_message[:3] + ".*"so bothre.matchandre.searchaccept it.Full suite: 128 passed.