This release adds improved error messages, more suggestions for existing checks, and 2 brand new checks!
Add remove-prefix-or-suffix
check (FURB188)
Don't explicitly check a string prefix/suffix if you're only going to remove it, use .removeprefix()
or .removesuffix()
instead.
Bad:
def strip_txt_extension(filename: str) -> str:
return filename[:-4] if filename.endswith(".txt") else filename
Good:
def strip_txt_extension(filename: str) -> str:
return filename.removesuffix(".txt")
Add use-reverse
check (FURB187)
Don't use x[::-1]
or reversed(x)
to reverse a list and reassign it to itself, use the faster in-place .reverse()
method instead.
Bad:
names = ["Bob", "Alice", "Charlie"]
names = reversed(names)
# or
names = list(reversed(names))
# or
names = names[::-1]
Good:
names = ["Bob", "Alice", "Charlie"]
names.reverse()
Detect itemgetter()
in FURB118 (use-operator
)
The operator.itemgetter()
function can be used to get one or more items from an object, removing the need to create a lambda just to extract values from an object:
Bad:
row = (1, "Some text", True)
transform = lambda x: (x[2], x[0])
Good:
from operator import itemgetter
row = (1, "Some text", True)
transform = itemgetter(2, 0)
Detect no-arg lambdas returning default literals in FURB111 (use-func-name
)
Don't use lambdas when you want a default value for a literal type:
Bad:
counter = defaultdict(lambda: 0)
multimap = defaultdict(lambda: [])
Good:
counter = defaultdict(int)
multimap = defaultdict(list)
Full Changelog: v1.27.0...v1.28.0