Stop treating % as a template in style option values - #1318
Open
rootkiller6788 wants to merge 2 commits into
Open
Stop treating % as a template in style option values#1318rootkiller6788 wants to merge 2 commits into
rootkiller6788 wants to merge 2 commits into
Conversation
ConfigParser interpolates % by default, so a value like NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS = "%" blew up with an InterpolationSyntaxError before any formatting happened. Style options aren't templates, so turn interpolation off when we build the parser from a dict, a command-line string, or a style file.
FormatErrorMsg assumed every exception carries a particular args shape and a .msg attribute. The vendored ParseError doesn't match that shape, so formatting valid-but-unsupported syntax (3.12+ f-strings and type aliases) ended in "IndexError: tuple index out of range" while building the message. Handle ParseError using its context and fall back to str(e) for anything else.
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.
Two small fixes I ran into while messing with style config and parse-error handling.
A style file with
NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS = "%"made yapf blow up before it formatted anything. ConfigParser treats%as an interpolation marker, but style option values are never templates. I disabled interpolation for the parsers built from style dicts,--stylestrings, and style files. With that, the setting behaves as expected:a = 1 % 2becomesa = 1%2. FixesNO_SPACES_AROUND_SELECTED_BINARY_OPERATORScrashes yapf when configured with%#1187.When yapf hit valid-but-newer Python (3.12+ f-strings reusing the quote,
type X = ...aliases), it crashed again while trying to build the error message, ending inIndexError: tuple index out of range. FormatErrorMsg was assuming an exception shape that the vendored ParseError doesn't have. It now reads the line/column off ParseError.context and falls back to str(e) for anything else, so you get a cleanyapf: <file>:1:36: bad inputinstead of a traceback. The code in those issues ([Bug][Crash][Reproducible] IndexError: tuple index out of range #1303, Ternary conditionals within F Strings Raise IndexError #1215, Crash on format strings with un-escaped quotes in the format element #1250, IndexError on type definition #1256, can't formatdict[str, Any]#1290) still won't be formatted until the grammar learns the new syntax, but at least the error is honest now.Added regression tests for both paths; the full yapftests suite passes.