Skip to content

Rule idea: Unnecessary use of re #12283

@JelleZijlstra

Description

@JelleZijlstra

I recently saw someone write re.sub('\\\$', '', s) when they could have just written s.replace('$', ''). Similarly, re.match("abc", s) could be replaced with s.startswith("abc"), re.search("abc", s) could be replaced with "abc" in s, re.fullmatch("abc", s) could be replaced with s == "abc", re.split("abc", s) could be replaced with s.split("abc").

Why is it better to use the str methods directly? It makes the code simpler and therefore easier to understand, may require less escaping, and it's probably usually faster.

Some thoughts on what the implementation could look like: We'd have to inspect the pattern string and ensure it doesn't contain any characters that are special for the regex engine, other than \ (for escaping) and in some cases ^ and $ for the beginning and end of the string. The implementation would also need to check whether any other flags are passed to the regex function (e.g., re.IGNORECASE would mean that a regex that contains alphabetical characters is no longer replaceable). There are other subtleties that I haven't fully thought through; for example, $ also matches right before a newline at the end of a string. An initial implementation could limit itself to simpler cases, and leave possible more complex cases for later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationhelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions