Skip to content

Format symmetric list concatenations in preview - #5259

Open
ColumbusLabs wants to merge 5 commits into
psf:mainfrom
ColumbusLabs:codex/black-260-symmetric-list-concat
Open

Format symmetric list concatenations in preview#5259
ColumbusLabs wants to merge 5 commits into
psf:mainfrom
ColumbusLabs:codex/black-260-symmetric-list-concat

Conversation

@ColumbusLabs

@ColumbusLabs ColumbusLabs commented Jul 25, 2026

Copy link
Copy Markdown

Description

Closes #260.

When a long expression is exactly two list displays or comprehensions joined by one top-level +, preserve its optional parentheses under --preview only when each operand fits on its own delimiter-split line. This lets the existing delimiter splitter place each operand on its own line instead of exploding only the right-hand list. Long operands, active magic-trailing-comma layouts, stable style, short expressions, chained concatenations, and mixed operands keep their current behavior.

The patch adds the preview feature flag, native format fixtures, schema entry, future-style documentation, changelog entry, and the required self-format dogfood change.

Validation:

  • env -u NO_COLOR tox -e py (480 passed, 1 skipped; Jupyter 73 passed)
  • focused symmetric-list fixtures (4 passed)
  • tox -e run_self,generate_schema
  • strict Sphinx build with -W
  • full pre-commit suite
  • 16,000-case list/line-length/magic-comma idempotence review matrix
  • git diff --check
  • independent review completed with no remaining findings

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?

@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (339735b) to main (e744e77):

--preview style (View full diff):
╭─────────────────────── Summary ────────────────────────╮
│ 6 projects & 8 files changed / 56 changes [+32/-24]    │
│                                                        │
│ ... out of 3 019 057 lines, 14 025 files & 22 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

--stable style: no changes


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

@cobaltt7 cobaltt7 left a comment

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.

Similar to #5252, I really like this change and I think it looks cleaner, but it's a net LOC increase. Would appreciate input from other maintainers!

On the other hand, it looks like once the multiple lines issue is fixed, this won't end up affecting much already written code, because the second array that's currently broken across multiple lines has a trailing comma and won't be condensed. Hence in practice this only affects people running with -C, list comprehensions, and unformatted code. With the decreased scope it makes it easier to merge quickly

Comment thread src/blib2to3/pgen2/tokenize.py Outdated
Comment thread src/black/lines.py Outdated
Comment thread src/black/lines.py
@ColumbusLabs
ColumbusLabs force-pushed the codex/black-260-symmetric-list-concat branch from ef7bead to 13165c2 Compare July 29, 2026 00:09
@ColumbusLabs
ColumbusLabs force-pushed the codex/black-260-symmetric-list-concat branch from 13165c2 to 79a44ca Compare August 5, 2026 09:49
Comment thread tests/data/cases/preview_symmetric_list_concatenation.py Outdated
Comment thread CHANGES.md Outdated
@cobaltt7

cobaltt7 commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator
values = (
    ["first_value", "second_value", "third_value"]  # abc
    + ["fourth_value", "fifth_value", "sixth_value"]
)

values = (
    ["first_value", "second_value", "third_value"]
    + ["fourth_value", "fifth_value", "sixth_value"]  # abc
)

Comments at the end of the line should not restrict splitting

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.

List concatenation, multiple lines

2 participants