Skip to content
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

Allow retry_if_exception_message to specify regex flags #436

Closed
ryancausey opened this issue Feb 5, 2024 · 3 comments
Closed

Allow retry_if_exception_message to specify regex flags #436

ryancausey opened this issue Feb 5, 2024 · 3 comments

Comments

@ryancausey
Copy link

Currently, the implementation of retry_if_exception_message accepts a regular expression pattern in the match argument. This gets passed to re.compile and used to perform a match against the exception message.

However, since the argument is a regular expression pattern and not a re.Pattern object, users are unable to specify any of the regular expression flags. For example, one cannot pass re.IGNORECASE to specify case-insensitive matching.

A proposed solution is to allow someone to pass an already compiled regular expression, a re.Pattern object, into the match argument so the caller has full control of the regular expression used, including flags.

Relates to:

@jd
Copy link
Owner

jd commented Feb 6, 2024

Not great but calling re.compile on a re.Pattern works.

@ryancausey
Copy link
Author

Oh, I didn't realize that would work. I'll close this then.

@asqui
Copy link
Contributor

asqui commented Feb 8, 2024

I guess we should update the parameter type to capture this non-obvious flexibility?

- match: typing.Optional[str] = None,
+ match: typing.Optional[str | re.Pattern] = None,

in

tenacity/tenacity/retry.py

Lines 198 to 205 in 17aefd9

class retry_if_exception_message(retry_if_exception):
"""Retries if an exception message equals or matches."""
def __init__(
self,
message: typing.Optional[str] = None,
match: typing.Optional[str] = None,
) -> None:

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

No branches or pull requests

3 participants