1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
from dataclasses import dataclass
from mypy.nodes import MemberExpr, NameExpr, RefExpr
from refurb.error import Error
@dataclass
class ErrorInfo(Error):
"""
Regex operations can be changed using flags such as `re.I`, which will make
the regex case-insensitive. These single-character flag names can be harder
to read/remember, and should be replaced with the longer aliases so that
they are more descriptive.
Bad:
```
if re.match("^hello", "hello world", re.I):
pass
```
Good:
```
if re.match("^hello", "hello world", re.IGNORECASE):
pass
```
"""
name = "use-long-regex-flag"
code = 167
categories = ("readability", "regex")
SHORT_TO_LONG_FLAG = {
"re.A": "re.ASCII",
"re.I": "re.IGNORECASE",
"re.L": "re.LOCALE",
"re.M": "re.MULTILINE",
"re.S": "re.DOTALL",
"re.T": "re.TEMPLATE",
"re.U": "re.UNICODE",
"re.X": "re.VERBOSE",
}
def check(node: NameExpr | MemberExpr, errors: list[Error]) -> None:
match node:
case RefExpr(fullname=fullname):
if long_name := SHORT_TO_LONG_FLAG.get(fullname):
errors.append(
ErrorInfo.from_node(node, f"Replace `{fullname}` with `{long_name}`")
)
|