alphabetize
OrderingAlphabetizes import siblings, dict-key blocks, and class-body members.
Blank lines carry rhythm, telling the reader where one unit ends and the next begins, with a consistent rhythm across a file letting the reader skim by section without parsing each statement.
normalizes the discipline around module-level definitions, class members, and theif __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, whatever top-level statement follows. 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 (any line a decorative rule of =, -, *, _, #, or ~, or a Markdown-style heading opening with two or more #) 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
import keyword.| Key | Type | Default | Meaning |
|---|---|---|---|
enabled | bool | true | Toggles 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 facet. Projects that want a different cadence can disable the rule and let their editor's blank-line conventions stand.
Two blank lines precede every module-level def and class, giving the reader's eye an anchor between top-level units.
for setup in initial_steps:
setup.run()
def main():
return 0
A module-level def followed by a bare call normalizes to two blank lines between them, the same after-definition cushion an assignment already received.
A Markdown-style heading comment whose body opens with two or more # reads as a section divider, not a description of the definition below. The canonical gap lands above the heading, with one blank line between the heading and the definition it leads.
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.
A bare import and a following from import already separated by one blank line stay at one, passing through untouched.