Skip to content

fix: keep blank line after nested def/class with one-line docstring - #393

Open
wolfgang-aura wants to merge 2 commits into
PyCQA:masterfrom
wolfgang-aura:fix/nested-definition-blank-lines
Open

wolfgang-aura wants to merge 2 commits into
PyCQA:masterfrom
wolfgang-aura:fix/nested-definition-blank-lines

Conversation

@wolfgang-aura

Copy link
Copy Markdown

Fixes #354.

A nested function or class whose docstring is a single line lost the blank line ruff and black expect before the next statement in the enclosing scope. With --blank, a nested class got two blank lines instead of one. The two formatters then kept undoing each other.

  • _get_function_docstring_newlines: when the next code is indented less than the docstring, it belongs to the enclosing scope, so keep one blank line.
  • _get_class_docstring_newlines: two blank lines only when the next code is at column 0; one for a nested class.

Added test_nested_definition_blank_line_kept, which checks that a nested def and a nested class round-trip unchanged, with and without --blank. tests/formatter and the unit-test modules pass, and black, isort, ruff and pydocstyle are clean.

Prepared with AI assistance (Claude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docformatter deadlock with ruff-format: blank lines around nested definitions

2 participants