Skip to content

Fix unstable formatting with comments on optional parens#5241

Open
l46983284-cpu wants to merge 2 commits into
psf:mainfrom
l46983284-cpu:fix-3701-unstable-multi-pass-comment
Open

Fix unstable formatting with comments on optional parens#5241
l46983284-cpu wants to merge 2 commits into
psf:mainfrom
l46983284-cpu:fix-3701-unstable-multi-pass-comment

Conversation

@l46983284-cpu

@l46983284-cpu l46983284-cpu commented Jul 17, 2026

Copy link
Copy Markdown

Description

Fixes multi-pass instability when an inline comment sits on optional parentheses (assert message / assignment RHS). Omitting those parens re-parented the comment onto a different leaf, so the RHS splitter chose a different shape on the next pass and tripped the second-pass stability check.

can_omit_invisible_parens now returns false when the optional opening paren already carries an inline comment, which pins the comment and keeps the formatting stable. Covers the #3701 reproducer and the same class from #3706 / #4384.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the
    stability policy?
    (N/A — this is an unstable-formatting bug fix, not a deliberate style change.)
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?
    (N/A — unintended formatting bug; no style-guide rewrite.)

Fixes #3701

Don't omit optional parentheses when the opening paren already carries
an inline comment. Omitting them re-parents the comment after the next
parse and can change the RHS split on a second pass (psf#3701, psf#3706,
psf#4384).

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu
l46983284-cpu force-pushed the fix-3701-unstable-multi-pass-comment branch from 5ffc355 to 3535e22 Compare July 17, 2026 11:41
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.

Unstable formatting: Comment is moved to lower line twice

1 participant