Fix fmt: skip failing to parse multiline case statements with backslash continuation#5123
Open
ParamChordiya wants to merge 5 commits intopsf:mainfrom
Open
Fix fmt: skip failing to parse multiline case statements with backslash continuation#5123ParamChordiya wants to merge 5 commits intopsf:mainfrom
ParamChordiya wants to merge 5 commits intopsf:mainfrom
Conversation
…sh continuation When # fmt: skip appears on a multiline case pattern using backslash line continuation (e.g. case A \ | B: # fmt: skip), the node traversal in _generate_ignored_nodes_from_fmt_skip stopped prematurely. The while loop used "\n" not in prefix to detect line boundaries, but the | token's prefix contains \<newline> (a line continuation), which was misread as a real line break. Only the second half of the or-pattern was collected, producing garbled output that failed to re-parse on the second formatting pass. Fix by adding _prefix_has_real_newline() which treats a newline preceded by a backslash as a continuation rather than a boundary. The traversal now walks back through the entire or-pattern and collects all nodes correctly. Fixes psf#5122
for more information, see https://pre-commit.ci
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.
Summary
casestatement with# fmt: skip#5122:black >= 26crashes withCannot parsewhen# fmt: skipis applied to a multilinecasepattern using backslash line continuation (\)_prefix_has_real_newline()helper to distinguish real newlines from backslash-continued ones_generate_ignored_nodes_from_fmt_skipto use the new helperfmtskip14.pycovering the reported scenario and multiple backslash continuationsRoot cause
_generate_ignored_nodes_from_fmt_skipwalks backward through the CST to collect all nodes on the same logical line as# fmt: skip. The while condition used"\n" not in current_node.prefixto detect a line boundary and stop.For a multiline
casepattern like:the
|token has prefix' \\\n '(backslash + newline). The old check treated this as a line boundary, so traversal stopped after collecting only the second group and the colon — missing the first group entirely. The resultingSTANDALONE_COMMENTwas malformed, and the second formatting pass crashed with a parse error.Fix
_prefix_has_real_newline(prefix)returnsTrueonly for bare newlines (not preceded by\). The while loop now continues through backslash continuations and collects the entire or-pattern correctly.Test plan
python -m pytest tests/test_format.py -q— 212 passed (includes newfmtskip14)python -m pytest tests/test_black.py -q --deselect tests/test_black.py::BlackTestCase::test_code_option_safe— 158 passed (pre-existing failure excluded)casestatement with# fmt: skip#5122 is left unchanged byblack