Checks for startswith or endswith calls on the same value with different prefixes or suffixes.
Why is this bad?
The startswith and endswith methods accept tuples of prefixes or suffixes respectively. Passing a tuple of prefixes or suffixes is more efficient and readable than calling the method multiple times.
Example
msg = "Hello, world!"
if msg.startswith("Hello") or msg.startswith("Hi"):
print("Greetings!")
Use instead:
msg = "Hello, world!"
if msg.startswith(("Hello", "Hi")):
print("Greetings!")
Fix safety
This rule's fix is unsafe, as in some cases, it will be unable to determine whether the argument to an existing .startswith or .endswith call is a tuple. For example, given msg.startswith(x) or msg.startswith(y), if x or y is a tuple, and the semantic model is unable to detect it as such, the rule will suggest msg.startswith((x, y)), which will error at runtime.
%
ruff rule PIE810
# https://docs.astral.sh/ruff/rules/multiple-starts-ends-withmultiple-starts-ends-with (PIE810)
Derived from the flake8-pie linter.
Fix is always available.
What it does
Checks for
startswith
orendswith
calls on the same value with different prefixes or suffixes.Why is this bad?
The
startswith
andendswith
methods accept tuples of prefixes or suffixes respectively. Passing a tuple of prefixes or suffixes is more efficient and readable than calling the method multiple times.Example
Use instead:
Fix safety
This rule's fix is unsafe, as in some cases, it will be unable to determine whether the argument to an existing
.startswith
or.endswith
call is a tuple. For example, givenmsg.startswith(x) or msg.startswith(y)
, ifx
ory
is a tuple, and the semantic model is unable to detect it as such, the rule will suggestmsg.startswith((x, y))
, which will error at runtime.References
str.startswith
str.endswith