Fix case guard splitting at small line lengths#5147
Merged
cobaltt7 merged 2 commits intoMay 22, 2026
Conversation
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
Collaborator
|
Thanks! |
Pedro-Muller29
added a commit
to Pedro-Muller29/black
that referenced
this pull request
May 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4280
Description
When
case case if Trueis formatted at a small line length, Black can make the pattern parentheses visible but also wrap the guard itself. That produces invalid output shaped likecase 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 ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?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.