Repository navigation
Keep commented expressions together when the comment trails the expression (preview) - #5660
Open
DresdenGman wants to merge 3 commits into
Open
DresdenGman wants to merge 3 commits into
DresdenGman wants to merge 3 commits into
Conversation
…ssion The keep_commented_expressions_together preview feature only deferred the delimiter split when a standalone comment preceded the first highest-priority delimiter. A standalone comment sitting after the expression inside the same brackets (e.g. after a comparison in parentheses) still fragmented the expression across lines. Also defer the delimiter split when the last standalone comment follows the last highest-priority delimiter at the same bracket depth, so the comment is split off first and each segment is formatted on its own. Comments interleaved between delimiters and trailing comments after comprehensions keep the delimiter split. Refs psf#638. The stable style follows once the preview feature is promoted.
for more information, see https://pre-commit.ci
Contributor
|
diff-shades results comparing this PR (b6d89c7) to main (81687b5):
|
cobaltt7
reviewed
Nov 5, 2026
Comment on lines
+1850
to
+1858
| # A standalone comment at either end of the bracketed expression | ||
| # forces a line break there anyway. Split at the comment first and | ||
| # let each resulting segment be formatted on its own, instead of | ||
| # fragmenting an expression that fits on one line (#638). The | ||
| # trailing comment must sit at the same bracket depth as the last | ||
| # delimiter: a comment inside a nested bracket is in the middle of | ||
| # the expression, not at its end. Comments interleaved between | ||
| # delimiters (such as between comprehension clauses) also keep the | ||
| # delimiter split, as does a trailing comment after a comprehension. |
Collaborator
There was a problem hiding this comment.
This comment is very long and wordy. The code should be largely self-explanatory. If clarification is essential and the code can't be rewritten, there may be short comments before the confusing lines - not a large block comment preceeding the entire check
Comment on lines
+95
to
+98
| - The `keep_commented_expressions_together` preview feature now keeps an expression | ||
| together wherever a standalone comment sits inside the surrounding brackets, not just | ||
| when the comment precedes the expression; a comment after a comparison in parentheses | ||
| no longer fragments it across lines (#5660) |
Collaborator
There was a problem hiding this comment.
Please avoid referencing a specific preview feature since they can't be triggered individually unless under --unstable (which this isn't).
You also don't need to include that they are "no longer fragmented", that's implied by saying it "keeps an expression together'
Shorter changelog entries are ideal
This branch has not been deployed
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.
Closes #638.
The preview feature
keep_commented_expressions_together(#5455) keeps a commented expression together when the comment comes before the expression, but an expression whose comment trails it — for example a comment after the expression, before the closing bracket — is still split apart, in preview as well as stable style.This completes the preview feature for the trailing case. In
delimiter_split, when the last standalone comment sits after the last highest-priority delimiter at the same bracket depth, and the split point is not a comprehension, the split now yields tostandalone_comment_split. Stable style output is unchanged, per the stability policy — the change only takes effect under--preview, and when the feature is promoted the remaining #638 shapes are covered with it.Tests: the full suite matches the baseline exactly (762 passed, 2 skipped, 61 subtests, no new failures), including 7 new preview data cases: comments before, after, and between operands, plus guards that overlong expressions still split and that magic trailing comma behavior is unchanged. Self-hosting check: black's own source tree is unchanged when formatted with the patched build.
One bookkeeping note: the CHANGES.md entry currently references the issue (#638); I will push a follow-up commit replacing it with this PR's number, per the convention at the top of that file.
Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?