-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[ruff
] Expand unnecessary-regular-expression
with re.compile
(RUF055)
#14680
Comments
I'm not sure if I fully understand the proposal. Is it to detect any usage of I'm asking because I'm not sure if the regex patterns can easily be replaced in the examples you linked because the compiled expressions are passed to some generic testing function. |
Hmm, I thought this sounded straightforward at first (just catch For example, this could trigger the rule: pat = re.compile("xyz")
if pat.match(s):
pass but we'd want something like this not to: pat = re.compile("xyz")
m = pat.match(s)
# use m ... Basically I think we'd need to extend #14679 to resolve string literals through a |
Indeed, the examples above are not as clear. A true positive example that would be great to be able to catch: https://github.com/great-expectations/great_expectations/blob/develop/ci/checks/check_only_name_tag_snippets.py#L48 |
I would say even that is not entirely clear-cut: because it's a global variable, it could be read from another module, and you don't know how that other module might use the |
Good point. Even though this example should use string operations instead of regexes, we can't be sure without analysing the other files. Some obvious cases: https://github.com/EmpireProject/Empire/blob/master/data/agent/agent.py#L927 After looking at more examples, I'm inclined to think that a dedicated rule with warning severity (#1256) that only considers the |
Often users use
re.compile
to compile a regex pattern. The returned regular expression object can be then used for the methods already caught by this rule. This is more efficient when the same pattern is reused.Detecting these cases can be done with the same logic as in RUF055. Providing a fix involves more work, as the compiled pattern can be dynamically passed. Having the detection without the fix is already valuable. Alternatively, this could be a separate rule.
without regex:
Examples:
cc: @ntBre
The text was updated successfully, but these errors were encountered: