Skip to content

Fix preview split for commented method chains - #5218

Open
ishaanlabs-gg wants to merge 2 commits into
psf:mainfrom
ishaanlabs-gg:fix-method-chain-comment-split
Open

Fix preview split for commented method chains#5218
ishaanlabs-gg wants to merge 2 commits into
psf:mainfrom
ishaanlabs-gg:fix-method-chain-comment-split

Conversation

@ishaanlabs-gg

Copy link
Copy Markdown

Description

Fixes #3998.

When a standalone comment appears between chained method calls, Black could split the
same leading call differently depending on whether another chained call followed the
comment. This adds a preview-gated formatter feature so commented method chains split
the first trailer consistently.

The change is intentionally limited to dotted delimiter splitting when the line contains
a standalone comment. Stable style remains unchanged.

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?

Validation:

  • PYTHONPYCACHEPREFIX=.pycache-tmp /Volumes/STOREJET/2/Dependencies/black-3998-venv/bin/python -m pytest tests/test_format.py -k preview_method_chain_comment -q
  • PYTHONPYCACHEPREFIX=.pycache-tmp /Volumes/STOREJET/2/Dependencies/black-3998-venv/bin/python -m pytest tests/test_docs.py tests/test_schema.py -q
  • PYTHONPYCACHEPREFIX=.pycache-tmp /Volumes/STOREJET/2/Dependencies/black-3998-venv/bin/python -m pytest tests/test_format.py -q
  • git diff --check

@ishaanlabs-gg
ishaanlabs-gg force-pushed the fix-method-chain-comment-split branch from cd5571e to e54ab69 Compare July 2, 2026 19:24
@cobaltt7

cobaltt7 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

This is a design decision, so we need to discuss the pros and cons of both styles.

@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (b747e2d) to main (74371e2):

--preview style (View full diff):
╭─────────────────────── Summary ────────────────────────╮
│ 4 projects & 11 files changed / 48 changes [+32/-16]   │
│                                                        │
│ ... out of 3 037 312 lines, 14 117 files & 22 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

--stable style: no changes


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

@cobaltt7

Copy link
Copy Markdown
Collaborator

Sorry, I misinterpreted the intended scope of this PR. After reviewing the original issue, it's clear this PR is meant to fix an issue I would personally consider a bug. However, the current implementation seems to be a bit overzealous. Here's some changes from the diff-shades report that seem detrimental, and the formatting should remain unchanged:
image
image

My understanding of the original issue is that fluent calls should never be split if they're a single call or if they fit inside the line length. If neither of those are true, then all calls in the expression should be split. The issue is that comments between multiple calls that should split can prevent the calls from splitting. It looks like this PR also allows comments before the entire fluent expression, or comments nested inside calls to cause the expression to be split.

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.

Inconsistent formatting of method call chain with comment

2 participants