Skip to content

blank-line-before-function-docstring (PDF205)

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 PDF204.

What it does

PDF205 requires exactly one blank line immediately before function, method, and nested-function docstrings.

Only the adjacent blank-line run before the docstring statement is changed. Comments, decorators, function headers, and non-blank lines before the docstring are preserved. The rule applies to ordinary functions, async functions, methods, and nested functions. It does not apply to class docstrings, attribute docstrings, or simple-suite docstrings written on the same physical line as the function header.

Why is this useful?

Some projects prefer visually separating the function header and any leading body comments from the docstring statement.

Ruff compatibility

This is the style-opposite companion to PDF204, which replaces Ruff's D201.

Examples

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

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

    """Docstring."""
    return None

Excess blank lines are collapsed to one, and leading comments stay attached above the inserted separator:

def function():
    # Leading comment.


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

    """Docstring."""
    return None

Decorated async functions are handled the same way:

@decorator
async def function():
    """Docstring."""
    return None
@decorator
async def function():

    """Docstring."""
    return None

Attribute docstrings are ignored:

def function():
    value = 1
    """Attribute docstring."""
    return value

Options

None.