reflow-parentheses
LayoutRemoves a grouping parenthesis pair that changes nothing, and breaks one whose joined form overflows code-line-length into a row per operand.
shed-backslash-continuations removes a trailing backslash and rejoins the statement it split, adding parentheses where the joined line would overflow the budget, so a multi-line statement breaks inside brackets everywhere and the reader meets one mechanism rather than a mix of escape characters and brackets. A backslash is the least legible way to split a Python statement, because it pins the continuation to a physical newline rather than to a bracketed group, and every layout rule then has to work around a break the author placed by hand.
What removing the backslash leaves behind depends on where it sits:
. or [ closes up rather than keeping a space before the operator.A rejoined line that would overflow the budget takes parentheses instead, wrapping the outermost expression the break falls inside and keeping the break where the author put it. Two breaks inside one expression share one pair rather than taking one each. A trailing comment moves onto the rejoined line, and its width counts toward the budget the rejoined line is measured against. Where no expression spans the break, as in an import list or an assert message, the statement rejoins regardless and line-overflow reports what no layout can bring within the budget. The one case left untouched is a backslash the lexer folds into a block's indentation, because that backslash sets the indent and no rejoin could keep it.
| Key | Type | Default | Meaning |
|---|---|---|---|
enabled | bool | true | Turns the rule on or off. |
x = 1 + \ and flag = ready and \ each split a binary expression across a trailing backslash, one at the arithmetic + and one at the boolean and. Each backslash is removed and the break closes, producing x = 1 + 2 and flag = ready and loaded on one line each, because the joined line fits within the budget.
x = 1 + 2
flag = ready and loaded
data = [ and result = call( each break across a bracket, and a backslash follows alpha, and first, inside the brackets. Each backslash is removed and the newline it sat on stays in place, because the bracket already spans the break and the backslash is redundant, leaving the layout rules to set the bracketed lines.
x = (2 + opens a parenthesized expression, and a backslash sits alone on the next physical line inside the parentheses before 2). The backslash and its now-empty line are both removed, because removing the backslash leaves nothing on that line, so the expression closes to x = (2 + over 2).
import os, sys, assert ready, "the resource never came up", and with open("a") as first, open("b") as second: each start out split across a backslash that carries no expression. Each rejoins onto one line, because none of the three breaks sits inside an expression, so no parenthesized form could keep the split.
The addition chain assigned to total spans a backslash, and joined onto one line it would overflow the budget. The outermost expression spanning the break is wrapped in parentheses instead of rejoined, so the six operands keep their two-line split and the parentheses carry the break the backslash used to.
x = 1 + \ continues onto a line ending in # note. The two lines join into x = 1 + 2 # note, with the comment kept at the end of the joined line, and the comment's width counts toward the budget the rule fits the joined line within, which sets whether a split rejoins or takes parentheses.
chained = value.first splits before each ., and sliced = mapping["first"] splits before [. Both rejoin with no space before the operator, to value.first.second.third and mapping["first"]["second"], because an attribute or subscript operator closes up against its operand.
An import statement splits collections_module_alpha from collections_module_beta_with_a_much_longer_name_that_overflows_here across a backslash. The two names rejoin onto one line even though it runs past the budget, because the break carries no expression, so no parenthesized form exists to keep the split, and in the default pipeline line-overflow would report the over-budget line this rule alone cannot bring within the budget.
x = 1 + \ continues across two consecutive backslashes before reaching 2, and y = alpha + \ continues across two more before gamma. Each run of backslashes folds as one rejoin, to x = 1 + 2 and y = alpha + beta + gamma, because the rule measures the line the whole run produces rather than one break at a time.
Two separate backslash runs sit on different rows of the same if condition, and the case runs with code-line-length = 40, so the joined condition would overflow. One pair of parentheses wraps the outermost expression spanning both runs and every backslash inside it is removed, rather than a pair around each run leaving the condition wrapped twice over.
Removes a grouping parenthesis pair that changes nothing, and breaks one whose joined form overflows code-line-length into a row per operand.
Expands a list, tuple, dict, or set literal across lines once it overflows code-line-length or a dict passes max-dict-entries, and rejoins a construct broken anywhere but an entry boundary.
Splits a from … import … that overflows import-line-length into repeated-prefix statements, breaks a comma-joined import a, b into one statement per module, and merges repeated from statements of one module into one line.
Reports a line still over its line budget once no layout rule can shorten it.
For per-statement opt-outs, the Suppression chapter covers the # prose: skip[shed-backslash-continuations] directive, which covers every line a continued statement spans.