PyCQA / PyCQA/flake8-bugbear

Proposed rules: encourage use of `re.compile` on constants in module scope, forbid it in function scope

Open
#536 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

I'd be interested in writing checks for this, if it would be acceptable.
Please just tell me / close this issue if these rules would not be welcome.

I often see re usage which would be made better by (1) using re.compile (rather than the free functions which take a string) or (2) moving re.compile from function scope out to module scope.[^1]

[^1]: Yes, it can be okay if the re cache covers your needs. But I've seen this in library code, where you cannot make any assumptions about how many regexes are being compiled and put in the cache.

Of course, for these changes to be correct, the regex string needs to be constant, but that's the common case.
The regex flags also need to be considered.

Example 1, encourage moving away from the free functions when a pattern can be pre-compiled:

# okay
def foo(x):
    return re.search(r"\w+", x)

# better
_WORD_PAT = re.compile(r"\w+")
def foo(x):
    return _WORD_PAT.search(x)

Example 2, there's no real reason to do it this way:

# bad, why write this? compile out in module scope
def foo(x):
    pat = re.compile(r"\w+")
    return pat.search(x)

(fix is the same as Example 1)

Example 3, can we handle flags? What if they're ORed together?

def foo(x):
    pat = re.compile(r"[abc\n]+", flags=re.I | re.M)
    return pat.search(x)

I therefore suggest two rules, one on by default and one in the opinionated category:

  • Opinionated: You should use re.compile() instead of {re.search, re.match, re.fullmatch, re.split, re.findall, re.finditer, re.sub, re.subn} on a string
  • Non-opinionated: You should not call re.compile() on a constant in function scope -- do it in class or module scope

I'd suggest starting with turning these rules on only where flags is omitted from the call, so the 1-argument form of re.compile and the 2-argument forms of the various functions. flags handling can be a later enhancement.


Implementation should be simple. Detecting these names would be similar to B017 -- just look for a node named "re" and don't worry about the name being rebound in funny ways.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing B017 implementation and how it identifies relevant AST nodes. Clarify the proposed rules' scope, including constant patterns, function versus module scope, and omitted or combined flags; done means the accepted behavior is defined for the examples and corresponding checks can be validated.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.