re.sub replaces a pattern without special characters, which str.replace does without a regular expression

tx0/python:regex-plain-substitutionstrictnoteperformance

What it finds

re.sub("-", "_", name) compiles and runs a regular expression for what name.replace("-", "_") does directly. Reported are calls of re.sub with three positional arguments whose pattern and replacement are string literals without any character that has a meaning in a pattern or a replacement, and without flags or a count.

From the rule's source, regex_plain_substitution.rs.

Example from the rule's tests
import re a = re.sub("-", "_", name)tx0 reports: this pattern has no special characters; str.replace does the sameb = re.sub(r"\s+", " ", name)c = re.sub("-", r"\1", name)d = re.sub("-", "_", name, count=1)
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:regex-plain-substitution" = "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-plain-substitution] reason="..."

tx0 explain tx0/python:regex-plain-substitution shows the same in the terminal. Found a false positive? Report it.