This release adds 2 new checks and adds improved detection and error messages for a few other checks!
Add no-copy-with-merge
Check (FURB185)
You don't need to call .copy()
on a dict/set when using it in a union since the original dict/set is not modified.
Bad:
d = {"a": 1}
merged = d.copy() | {"b": 2}
Good:
d = {"a": 1}
merged = d | {"b": 2}
Add use-sort
Check (FURB186)
Don't use sorted()
to sort a list and reassign it to itself, use the faster in-place .sort()
method instead.
Bad:
names = ["Bob", "Alice", "Charlie"]
names = sorted(names)
Good:
names = ["Bob", "Alice", "Charlie"]
names.sort()
Color is now disabled when piping output
Previously Refurb would always emit color unless you turned it off. Now it will check if the output is a TTY, and if so, disable the color.
Improve use-chain-from-iterable
(FURB179)
FURB179 now detects the following snippets:
functools.reduce(operator.add, rows)
functools.reduce(operator.add, rows, [])
functools.reduce(operator.concat, rows)
functools.reduce(operator.concat, rows, [])
These snippets are slow because they use the add
/concat
operator. This will create a new list for each iteration instead of doing it in-place.
Improve use-dict-union
(FURB173)
FURB173 now detects the following snippets:
_ = dict(**x)
_ = dict(x, **y)
_ = dict(**x, **y)
_ = dict(x, a=1)
_ = dict(**x, a=1, b=2)
_ = dict(**x, **y, a=1, b=2)
These can be re-written using dictionary unions:
_ = {**x}
_ = x | y
_ = x | y
_ = x | {"a": 1}
_ = x | {"a": 1, "b": 2}
_ = x | y | {"a": 1, "b": 2}