jd / tenacity

Retrying library for Python
http://tenacity.readthedocs.io
Apache License 2.0
6.82k stars 283 forks source link

Allow retry_if_exception_message to specify regex flags #436

Closed ryancausey closed 10 months ago

ryancausey commented 10 months ago

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 commented 10 months ago

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

ryancausey commented 10 months ago

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

asqui commented 9 months ago

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 https://github.com/jd/tenacity/blob/17aefd96cbc90fd5201f61562640709a185e8796/tenacity/retry.py#L198-L205