Skip to content

blank-lines

Blank lines carry rhythm. They tell the reader where one unit ends and the next begins, and a consistent rhythm across a file lets the reader skim by section without parsing each statement.

normalizes the discipline around module-level definitions, class members, and the if __name__ == "__main__": guard, so every file in the project reads with the same cadence.

Module-level def and class carry two blank lines before them and two after when followed by a top-level assignment. Methods inside a class body carry one, a module-level statement after if __name__ == "__main__": carries one, and adjacent bare-import and from-import groups carry one between them. Inside function bodies the rule leaves blank-line discipline alone, since the in-body rhythm remains a per-author choice. A description-shaped own-line comment block above a statement binds tight against the following statement, reading the comment as a description of the statement it precedes, whereas a banner-shaped block (with any line a decorative rule of =, -, *, _, #, or ~) keeps 1 blank line below to read as a section divider. The canonical above-gap is measured from the topmost comment in the block either way. The import surface sits downstream of

(which orders the entries first) and (which decides which packages keep the bare form), then this rule lands the blank-line separators between groups, and closes the sequence by aligning the import keyword.

Configuration

KeyTypeDefaultMeaning
enabledbooltrueToggle the rule on or off

The canonical blank-line counts are hard-coded to PEP 8's 2-between-top-level and 1-between-methods cadence, so the rule carries enabled as its only knob. Projects that want a different cadence can disable the rule and let their editor's blank-line conventions stand.

The Canonical Case

Two blank lines precede every module-level def and class, giving the reader's eye an anchor between top-level units.

python
for setup in initial_steps:
    setup.run()


def main():
    return 0

More Examples

A module-level class followed by an assignment normalizes to two blank lines between them, regardless of the source's actual count.

A banner-style block of own-line comments above a def reads as a section divider, not a description of the def. The canonical gap lands above the banner, with one blank line between the banner and the def below.

A comment between two statements reads as the second statement's leading block. The canonical gap normalizes above the comment and the comment binds tight against the following statement, collapsing any blank-count drift in the source.

A module-level def with a leading decorator has its leading edge counted from the decorator line. The blank-line count normalizes above the decorator, so the decorator travels with the def below it.

A bare import followed by a from import with two blank lines between them collapses to one, the canonical separation between adjacent import groups.

Two methods inside a class body normalize to one blank line between them, collapsing extra blanks and expanding missing ones to the same canonical count.

A bare import butted directly against a from import with zero blank lines expands to one blank line, splitting the two import kinds into distinct groups.

A module-level statement following an if __name__ == "__main__": block carries one blank line of separation, the main-guard cushion rather than the wider def-after-statement gap.

A class carrying its own docstring (here in triple-apostrophe quotes) gets one blank line before its first method. The class-scope canonical fires on the (docstring, method) pair, collapsing any wider drift below.

No Change

A bare import and a following from import already separated by one blank line stay at one, passing through untouched.