In stub files, enforce a blank line between a function and a decorated class definition#5092
Open
AlexWaygood wants to merge 4 commits intopsf:mainfrom
Open
In stub files, enforce a blank line between a function and a decorated class definition#5092AlexWaygood wants to merge 4 commits intopsf:mainfrom
AlexWaygood wants to merge 4 commits intopsf:mainfrom
Conversation
AlexWaygood
commented
Apr 11, 2026
Contributor
|
diff-shades results comparing this PR (49f7333) to main (0eddb3d):
|
Contributor
Author
|
The diff-shades preview changes all look like clear improvements to me |
MeGaGiGaGon
reviewed
Apr 11, 2026
for more information, see https://pre-commit.ci
MeGaGiGaGon
approved these changes
Apr 11, 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 already enforces a blank line before a (decorated or undecorated) class definition in many situations. However, it fails to do so if the decorated class definition is immediately preceded by a function.For this example file, saved as
foo.pyi:Black applies this formatting on
mainwith--previewOn this PR branch, the formatting changes to:
which feels much more consistent.
The fix adds a
_decorator_decorates_class()helper that walks the AST to check whether a decorator line decorates aclassdefnode, and uses it to insert a blank line in_maybe_empty_lines_for_class_or_defwhen appropriate.Fixes #4256. The same bug was fixed in the Ruff formatter in June, in astral-sh/ruff#18888
Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?