Skip to content

docstring-expand โ€‹

A single-line docstring (opener, body, and closer all on one line) reads as a kind of inline comment, and many downstream tools (Sphinx, IDE preview surfaces, doctest, PEP 257-aware linters) treat it inconsistently with its multi-line sibling.

expands every single-line triple-quoted docstring into the canonical multi-line shape, so a project's documentation surface presents one consistent structure across every documented unit.

The rule fires on module, class, and function single-line docstrings. The body content is preserved verbatim across the expansion, and the resulting multi-line form passes immediately to

for the opener-and-closer placement and to for the line-budget wrap.

The walker Docstring reads against the PEP 257 definition, so f-string forms (f"""...""") and concatenated string forms never qualify as docstrings and the rule skips them. Raw-prefixed (r""") and byte-prefixed (b""") single-line docstrings expand the same way as plain triple-quoted forms, with the prefix preserved verbatim on the opener. An empty single-line docstring ("""""") expands to a multi-line shape with a blank line between the opener and the closer. The PEP 257 summary-line convention is out of scope for this rule, leaving the body content's shape to authors and downstream conventions.

Configuration โ€‹

KeyTypeDefaultMeaning
enabledbooltrueToggle the rule on or off

The Canonical Case โ€‹

A module-level single-line docstring (opener, body, and closer all on one line) expands into the multi-line block shape, lifting the body onto its own line at column zero.

python
# This fixture pins module-level single-line docstring rewriting at
# column 0. The fixture's module docstring is itself the rewrite
# target, so the conventional triple-quoted claim-banner is replaced
# by these `#` comments.


"""
Module summary on a single line.
"""


x = 1

More Examples โ€‹

A class-level """A simple bag.""" splits across three lines with the body line landing at the class-body indent, sitting exactly where any other class-scope statement would. The rewrite tracks the enclosing scope's column rather than a fixed offset.

A method docstring expands with the method-body indent, two steps deeper than the module column. The body line lands at the same column as return not self.items, confirming the rewrite reads the actual enclosing indent rather than counting from column 0.

Outer and inner function defs each carry a single-line docstring, and the rewrite fires independently at every nesting level. Each expanded body line settles at the indent of its own def, so the inner docstring sits one step deeper than the outer.

A function's """Hello, world.""" becomes the canonical three-line shape with opener, body, and closer each on their own line. The body text passes through unchanged, trimmed of surrounding whitespace and re-laid at the def's body indent.

For the docstring budgets that govern wrapping, the Configuration chapter covers the description and structured line lengths.