A list or set comprehension only copies the items of an iterable

tx0/python:copying-comprehensionstrictwarningstyle

What it finds

[x for x in items] builds a list of the items as they are; list(items) says the same in fewer words and runs faster. Comprehensions with a condition, more than one for, an async for or a changed item are not affected. The fix writes the call; it is unsafe, since list or set may be bound to something else where the fix lands.

From the rule's source, copying_comprehension.rs.

Example from the rule's tests
def copy(items, pairs):    a = [x for x in items]tx0 reports: this comprehension only copies its iterable; list(items) says the same    b = {x for x in items}tx0 reports: set(items) says the same    c = [x for x in items if x]    d = [x * 2 for x in items]    e = [y for x in items for y in x]    f = [k for k, v in pairs]    return a, b, c, d, e, f
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:copying-comprehension" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:copying-comprehension] reason="..."

tx0 explain tx0/python:copying-comprehension shows the same in the terminal. Found a false positive? Report it.