fix(reformatter): ensure idempotent formatting for dictionary values with line continuation - #1320
Open
jdymitarai wants to merge 1 commit into
Open
Conversation
…ntinuations When formatting dictionary values preceded by a line continuation backslash under INDENT_DICTIONARY_VALUE (e.g. in the google style): 1. In continuation_splicer.py, use node.insert_child instead of node.children.insert to properly set the parent attribute on continuation nodes, preventing an AssertionError when navigating or replacing nodes. 2. In subtype_assigner.py, check for format_token.CONTINUATION alongside grammar_token.COMMENT so that dictionary values preceded by a continuation marker are correctly encompassed in an atom node when inserting pseudo-parentheses. 3. In reformatter.py, avoid appending redundant spaces in _FormatFinalLines for pseudo-parentheses when the succeeding token's value already starts with spaces (as continuation tokens do), preventing repeated space addition on each run. 4. In logical_line.py, prioritize continuation checks in _SpaceRequiredBetween before checking pseudo-parentheses. Fixes google#1217
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
Fixes #1217 where yapf repeatedly added spaces before a line continuation backslash (
\) in dictionary values on subsequent formatting runs (under styles enablingINDENT_DICTIONARY_VALUE, such asgoogle). Also resolves anAssertionErrorwhen dictionary values consisting of a single leaf were preceded by a line continuation backslash.Cause & Analysis
continuation_splicer.py, continuation nodes were inserted into pytree nodes vianode.children.insert()directly instead ofnode.insert_child(), leavingcontinuation_node.parentasNone. Any subsequent AST transformations or sibling queries (next_sibling,replace) on these nodes could fail or raise anAssertionError.subtype_assigner.py,_InsertPseudoParenthesesonly checkedgrammar_token.COMMENTwhen looking for tokens inserted before a value leaf, missingformat_token.CONTINUATION. As a result, single-leaf values preceded by a backslash failed to be encompassed into an atom node alongside the continuation marker.reformatter.py,_FormatFinalLinesappends a space for pseudo-parentheses if the succeeding token'swhitespace_prefixdoes not start with whitespace. Because continuation tokens hold their leading whitespace in their token value (' \') rather thanwhitespace_prefix,_FormatFinalLinesemitted an additional space before the continuation token on every formatting run, causing infinite drift.logical_line.py, continuation tokens were checked after pseudo-parentheses in_SpaceRequiredBetween, allowing pseudo scope opening to request an unnecessary space before continuation markers.Solution
node.insert_child(...)incontinuation_splicer.pyto maintain consistent parent links.format_token.CONTINUATIONalongsidegrammar_token.COMMENTin_InsertPseudoParentheses.not tok.next_token.value.startswith(' ')in_FormatFinalLinesbefore appending spaces for pseudo tokens.logical_line._SpaceRequiredBetween.reformatter_basic_test.pyverifying multiple-pass idempotency and single-leaf dictionary value continuation handling.