Add blank lines after docstring-only functions in stub files outside overload groups#5158
Open
AlexWaygood wants to merge 3 commits into
Open
Add blank lines after docstring-only functions in stub files outside overload groups#5158AlexWaygood wants to merge 3 commits into
AlexWaygood wants to merge 3 commits into
Conversation
AlexWaygood
commented
May 30, 2026
This comment was marked as resolved.
This comment was marked as resolved.
cobaltt7
approved these changes
Jun 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
In
.pyistub files, Black currently does not consistently separate a function or method with a docstring-only body from the statement that follows it. It already inserts a blank line when another function definition follows directly, but not when the next statement is a comment, conditional block, variable annotation, or other non-function statement:This PR adds a new preview feature (pyi_blank_line_after_function_docstring) that enforces a blank line after a function or method body that consists only of a docstring in .pyi files, making Black more consistent about inserting blank lines after function docstrings:
The implementation teaches
EmptyLineTrackerto detect this case when the previous statement was a definition whose body ended in a docstring. It preserves the existing special handling for same-name decorated functions, so@overload groupsand@propertysetters are still kept together without extra blank lines between the related definitions.Motivation
Most libraries don't include docstrings in their stub files, but it's increasingly popular to monkeypatch them in as a build step using tools like docstring-adder and docify. This is what ty does for its vendored typeshed stubs, for example; this patch greatly improves readability for our vendored, docstringified stubs, in my opinion. An example branch with the formatting from this PR applied is here: https://github.com/astral-sh/ruff/compare/main...AlexWaygood:alex/new-stub-formatting?expand=1
Checklist - did you ...