Skip to content

BUG: Rule D413 does not catch missing single blank line or multiple blank lines at end of docstring #13284

@user27182

Description

@user27182

There is a bug with rule D413. With this rule, a single blank line is expected at the end of multiline docstrings.

But, this does not work in a few cases. E.g.:

Calling ruff check foo.py --select D413 --fix on this code does not insert a blank line at the end:

def foo():
    """Bar.

    Multiline docstring.
    """

In another case, we see that this rule works for some section names such as Returns, e.g. here a new line is correctly inserted at the end:

def foo():
    """Bar.

    Returns
    -------
    Nothing.
    """

But, it does not work for any general section heading. E.g. no blank line is inserted here after the Section section (but one is expected):

def foo():
    """Bar.

    Section
    -------
    Nothing.
    """

For the first case, I suppose there may be an argument that technically the docstring has no sections, and therefore the rule isn't being violated. And for the last case there may be an argument that only standard section names are supported (e.g. Parameters, Returns, See Also, etc.). But, in my view, I think that with this rule enabled, there should always be a blank line expected (and inserted with --fix) at the end of the docstring.

I have not tested this against the pydocstyle behavior though, not sure what the expected result is from that. Maybe a new rule could be added instead in case the current behavior is considered to be "correct"?

Also, related to #9451, with D413 we should expect only a single line at the end of a docstring. But this code passes (the two blank lines should be replaced with a single blank line):

def foo():
    """Bar.

    Multiline docstring.


    """

Same thing with this code, only one line is expected:

def foo():
    """Bar.

    Multiline docstring.

    Returns
    -------
    Nothing.

    
    """

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingBreaking API changedocstringRelated to docstring linting or formattingneeds-decisionAwaiting a decision from a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions