collection-literal-concatenation (RUF005)
Fix is sometimes available.
What it does
Checks for uses of the +
operator to concatenate collections.
Why is this bad?
In Python, the +
operator can be used to concatenate collections (e.g.,
x + y
to concatenate the lists x
and y
).
However, collections can be concatenated more efficiently using the
unpacking operator (e.g., [*x, *y]
to concatenate x
and y
).
Prefer the unpacking operator to concatenate collections, as it is more
readable and flexible. The *
operator can unpack any iterable, whereas
+
operates only on particular sequences which, in many cases, must be of
the same type.
Example
Use instead:
Fix safety
The fix is always marked as unsafe because the +
operator uses the __add__
magic method and
*
-unpacking uses the __iter__
magic method. Both of these could have custom
implementations, causing the fix to change program behaviour.