[ruff] re and regex calls with unraw string as first argument (RUF039)#14446
[ruff] re and regex calls with unraw string as first argument (RUF039)#14446MichaReiser merged 11 commits intoastral-sh:mainfrom
ruff] re and regex calls with unraw string as first argument (RUF039)#14446Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF039 | 199 | 199 | 0 | 0 | 0 |
There was a problem hiding this comment.
Thanks. This overall looks good. I'm interested in your and @AlexWaygood's opinion on flagging non-raw strings for all regex patterns or if we should make the rule slightly more clever, e.g. by allowing regex patterns containing an escape sequence that can't be written as a raw string (e.g. \n)
|
I think this should be a blanket enforcement. # It's too easy to use a normal string as the pattern...
re.compile('uv') # `uv` anywhere (`uv`, `uvicorn`, `dhruv`, `juvenile`, etc.)
# ...then forget to switch to a raw string when the pattern changes.
re.compile('\buv\b') # `uv` not within a word? |
|
I don't disagree with this overall, but it does mean that it gives you false positives for e.g. |
|
So maybe that's specific to escape sequences. Python regex support all other common escape sequences (with the exception of |
|
Oh,
|
|
I haven't studied this in depth, but it overall seems reasonable to me, both in concept and implementation. I wondered if we needed to take account of this footgun to do with raw strings (they cannot end with an odd number of backslashes). But I think we should be fine, since that documentation states:
|
ruff] re and regex calls with unraw string as first argument (RUF051)ruff] re and regex calls with unraw string as first argument (RUF039)
Summary
Resolves #11167.
Test Plan
cargo nextest runandcargo insta test.