ruff icon indicating copy to clipboard operation
ruff copied to clipboard

PERF402 slightly incorrect fix suggestion

Open Skylion007 opened this issue 2 years ago • 1 comments

a = []
for i in range(10):
    for j in range(100):
        a.append(j)

ruff version 0.1.0

This rule outputs test-perf.py:4:9: PERF402 Use `list` or `list.copy` to create a copy of a list Which while correct in that something should be done here, it really should be calling .extend() not the list or list.copy() constructor. Doing

a = []
for i in range(10):
    a = list(range(100))

would yield an invalid result.

Skylion007 avatar Oct 19 '23 17:10 Skylion007

Do we want to specify that this is only really relevant for nested if-loops? Or do we also want to suggest it for single loops like in the default example in our docs and upstream implementation.

Right now PERF402 docs describe converting from:

/// original = list(range(10000))
/// filtered = []
/// for i in original:
///     filtered.append(i)

to

/// original = list(range(10000))
/// filtered = list(original)

([NIT] that filtered name is kind of misleading too here) Do we want this to become:

/// original = list(range(10000))
/// filtered = [].extend(original)

?

qdegraaf avatar Oct 25 '23 12:10 qdegraaf