Skip to content

blank-line-after-function-docstring (PDF207)

Added in 1.0.0 Always fix Convention-explicit

Part of the PDF rule category.

View source · View documentation source · Search issues

Fix is always available.

Rule is ignored by broad selectors for all docstring-convention values.

Rule is incompatible with PDF206.

What it does

PDF207 requires exactly one blank line immediately after function, method, and nested-function docstrings when another statement follows in the same function body.

Only the adjacent blank-line run after the docstring statement is changed. Comments and following statements are preserved. The rule applies to ordinary functions, async functions, methods, and nested functions. It does not apply to class docstrings, attribute docstrings, simple-suite docstrings, or function bodies where the docstring is not followed by another body statement.

Why is this useful?

Some projects prefer visually separating function docstrings from executable body statements.

Ruff compatibility

This is the style-opposite companion to PDF206, which replaces Ruff's D202.

Examples

The canonical fix inserts one blank line after a function docstring:

def function():
    """Docstring."""
    return None
def function():
    """Docstring."""

    return None

Excess blank lines are collapsed to one. For multiline docstrings, the blank line is placed after the closing quote line:

def function():
    """Summary.

    Body.
    """
    return None

def other():
    """Docstring."""


    return None
def function():
    """Summary.

    Body.
    """

    return None

def other():
    """Docstring."""

    return None

A following comment is treated as the next body statement, so the blank line is inserted before that comment:

def function():
    """Docstring."""
    # Body comment.
    return None
def function():
    """Docstring."""

    # Body comment.
    return None

Docstrings without a following body statement and simple-suite docstrings are ignored:

def function():
    """Docstring."""
    # Body comment.

def simple(): """Docstring."""; return None

Options

None.