Skip to content

shed-backslash-continuations

A trailing backslash is the least legible way to split a Python statement, because the continuation is pinned to a physical newline rather than to a bracketed group, leaving every layout rule to work around a break the author placed by hand.

removes the escape and settles the split through brackets instead, so a reader meets one uniform mechanism for a multi-line statement rather than a mix of escape characters and brackets.

Where a bracket already spans the break, the backslash carries nothing and simply goes, leaving the newline for

and its siblings to shape. A backslash occupying a whole physical line takes that line with it, since nothing survives its removal. Everywhere else the statement rejoins onto one line, with the separator inserted only where one belongs, so a chain split ahead of . or [ closes up rather than stranding 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. Where no expression spans the break, as in an import list or an assert message, the statement rejoins regardless and

flags what no reshape can bring within. The one shape left untouched is a backslash the lexer folds into a block's indentation, which declares that indent and survives no rejoin.

Configuration

KeyTypeDefaultMeaning
enabledbooltrueToggles the rule on or off.

The Canonical Case

A binary expression broken across a trailing backslash rejoins onto one line, so x = 1 + \ and its continuation read as x = 1 + 2.

x = 1 + 2

flag = ready and loaded
python

More Examples

A bracket carries the break on its own, so the backslash inside a list or a call drops and the newline stays for the layout rules to shape.

A backslash occupying a whole physical line inside brackets leaves nothing behind once it goes, so the empty line goes with it.

An import list, an assert message, and a with item list carry no expression across the break, so each rejoins onto one line rather than taking parentheses.

Where the rejoined line would overflow the budget, the outermost expression spanning the break is parenthesized instead, so the bracketed form carries the split the backslash used to.

A comment closing the continuation line joins onto the merged line with the code it annotates, and its width counts toward the budget that decides between rejoining and parenthesizing.

A rejoin inserts a separator only where one belongs, so a chain split before . or [ closes to value.first.second.third rather than leaving a space ahead of the operator.

An import list carries no expression across the break, so no parenthesized form is available and the statement rejoins even though the joined line runs past the budget, leaving

to flag what no reshape reaches.

Consecutive backslashes across one logical line are weighed as a single rejoin, so the run answers the budget with the line it actually produces rather than one break at a time.

For per-statement opt-outs, the Suppression chapter covers the # prose: skip[shed-backslash-continuations] directive, which holds every line a continued statement spans.