Skip to content

python/comprehension-multi-line

Rule comprehension-multi-line
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

A comprehension that runs over more than one line is hard to read. Write a loop, or give the expression for one item a name and a function.

def stacks(trades):
return {
str(lot): Stack(group["spread"].to_numpy(), group["mw"].cumsum().to_numpy())
for lot, group in trades.groupby("lot", sort=False)
}
found = {}
for lot, group in trades.groupby("lot", sort=False):
spread = group["spread"].to_numpy()
depth = group["mw"].cumsum().to_numpy()
found[str(lot)] = Stack(spread, depth)
return found

The rule reports a comprehension or generator expression that runs over more than one line.

xpd has no automatic fix, so make the change by hand.

A comprehension that needs several lines holds more than one step, and its result stands before the loop that makes it, so a reader goes through it twice. The loop gives each step its own line and its own name, in the order it runs.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This comprehension runs over more than one line. Write a loop, or give the expression for one item a name and a function.

Browse the other rules of the Python pack.