Visitar URL original
Keep commented expressions together when the comment trails the expression (preview) by DresdenGman · Pull Request #5660 · psf/black · GitHub
Skip to content

Keep commented expressions together when the comment trails the expression (preview) - #5660

Open
DresdenGman wants to merge 3 commits into
psf:mainfrom
DresdenGman:fix-638-keep-commented-expressions-together
Open

DresdenGman wants to merge 3 commits into
psf:mainfrom
DresdenGman:fix-638-keep-commented-expressions-together

Conversation

@DresdenGman

@DresdenGman DresdenGman commented Nov 4, 2026 •

Copy link
Copy Markdown

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 to standalone_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 ...

  • Implement any code style changes under the --preview style, following the stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

DresdenGman and others added 3 commits November 4, 2026 12:58
…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.
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (b6d89c7) to main (81687b5):

--preview style (View full diff):
╭─────────────────────── Summary ────────────────────────╮
│ 5 projects & 10 files changed / 41 changes [+13/-28]   │
│                                                        │
│ ... out of 3 166 228 lines, 14 200 files & 22 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

Comment thread src/black/linegen.py
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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread CHANGES.md
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
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.

Bug: linebreak in comparison in parens with comments in conditional is not stable

2 participants