Is there a lint rule for python that automatically detects list +operator concatenation and suggests using spread

65 Views Asked by At

For me the following

extras = ["extra0", "extra1"]
func_with_list_arg([
    "base0",
    "base1",
] + extras)

is nicer to read with a spread operator like the following

extras = ["extra0", "extra1"]
func_with_list_arg([
    "base0",
    "base1",
    *extras,
])

Is there a lint rule in ruff or pylint that would detect this situation?

1

There are 1 best solutions below

0
Guillaume On BEST ANSWER

Yes, there is!

c = [3]
b = [1, 2] + c
print(b)

When I run (my configured) ruff on this, I get:

RUF005 [*] Consider [1, 2, *c] instead of concatenation

This is part of the ruff specific ruleset: https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf and is a rule which is actually automatically fixable.