Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#536 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu bằng cách xem xét triển khai B017 hiện có và cách nó xác định các nút AST liên quan. Làm rõ phạm vi của các quy tắc được đề xuất, bao gồm các mẫu hằng, phạm vi hàm so với phạm vi mô-đun, cũng như các flag bị bỏ qua hoặc được kết hợp; được xem là hoàn tất khi hành vi được chấp nhận đã được xác định cho các ví dụ và các kiểm tra tương ứng có thể được xác thực.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
1.1k
Fork
131
Merge trung bình
1 ngày 21 giờ
Pull request đã merge (30 ngày)
26

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của PyCQA/flake8-bugbear

Tất cả issue của PyCQA/flake8-bugbear

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.