A pattern spells out a character class that has a shorthand, such as [0-9] for \d
What it finds
\d, \w and \s are shorter and read at a glance; [0-9] and [a-zA-Z0-9_] make readers check the ranges. In Python 3, \d and \w also match digits and letters of other scripts unless re.ASCII is given, which is worth knowing when replacing them.
From the rule's source, regex_class_shorthand.rs.
import re a = re.compile(r"[0-9]+")tx0 reports: [0-9] can be written \db = re.compile(r"[a-zA-Z0-9_]+")tx0 reports: [a-zA-Z0-9_] can be written \wc = re.compile(r"[0-7]+") Turn it on, off or suppress it
The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:regex-class-shorthand" = "note"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:regex-class-shorthand] reason="..."tx0 explain tx0/python:regex-class-shorthand shows the same in the terminal. Found a false positive? Report it.