Skip to content

Fix case guard splitting at small line lengths#5147

Merged
cobaltt7 merged 2 commits into
psf:mainfrom
puneetdixit200:fix/case-guard-small-line-length
May 22, 2026
Merged

Fix case guard splitting at small line lengths#5147
cobaltt7 merged 2 commits into
psf:mainfrom
puneetdixit200:fix/case-guard-small-line-length

Conversation

@puneetdixit200
Copy link
Copy Markdown
Contributor

@puneetdixit200 puneetdixit200 commented May 21, 2026

Fixes #4280

Description

When case case if True is formatted at a small line length, Black can make the pattern parentheses visible but also wrap the guard itself. That produces invalid output shaped like case case ( if True ):, and the second formatting pass fails to parse it.

This keeps guard nodes from being wrapped as a whole and allows the case-pattern split to keep the visible pattern parentheses instead. The guard remains outside the pattern parentheses, matching the expected output from the issue.

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?

Tests run:

  • .\.venv\Scripts\python -m pytest tests/test_format.py -k pattern_matching_case_case_small_line_length -q
  • .\.venv\Scripts\python -m pytest tests/test_format.py -k pattern_matching -q
  • .\.venv\Scripts\python -m pytest tests/test_format.py -q
  • .\.venv\Scripts\python -m black --check src\black\linegen.py
  • .\.venv\Scripts\python -m pytest tests -q --run-optional no_jupyter -k "not symlink"

I also ran .\.venv\Scripts\python -m pytest tests -q --run-optional no_jupyter; only three existing symlink tests failed because this Windows environment cannot create symlinks.

Puneet Dixit added 2 commits May 22, 2026 02:29
Keep guards outside the visible pattern parentheses when a case pattern splits, avoiding invalid output for case-pattern names that are also soft keywords.

Fixes psf#4280
@github-actions
Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (0da5688) to main (7613840):

--preview style: no changes

--stable style: no changes


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

@cobaltt7
Copy link
Copy Markdown
Collaborator

Thanks!

@cobaltt7 cobaltt7 merged commit 13e97b4 into psf:main May 22, 2026
57 checks passed
Pedro-Muller29 added a commit to Pedro-Muller29/black that referenced this pull request May 23, 2026
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.

Crash on formatting case case syntax when line length set to be small

2 participants