slice-to-remove-prefix-or-suffix (FURB188)#
Derived from the refurb linter.
Fix is always available.
This rule is unstable and in preview. The --preview
flag is required for use.
What it does#
Checks for the removal of a prefix or suffix from a string by assigning
the string to a slice after checking .startswith()
or .endswith()
, respectively.
Why is this bad?#
The methods [str.removeprefix
][str.removeprefix] and [str.removesuffix
][str.removesuffix],
introduced in Python 3.9, have the same behavior
and are more readable and efficient.
Example#
Use instead:
[str.removeprefix
][str.removeprefix]: https://docs.python.org/3/library/stdtypes.html#str.removeprefix
[str.removesuffix
][str.removesuffix]: https://docs.python.org/3/library/stdtypes.html#str.removesuffix