Skip to content

Stop treating % as a template in style option values - #1318

Open
rootkiller6788 wants to merge 2 commits into
google:mainfrom
rootkiller6788:fix-percent-style-config-and-error-msg
Open

Stop treating % as a template in style option values#1318
rootkiller6788 wants to merge 2 commits into
google:mainfrom
rootkiller6788:fix-percent-style-config-and-error-msg

Conversation

@rootkiller6788

Copy link
Copy Markdown

Two small fixes I ran into while messing with style config and parse-error handling.

  1. 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, --style strings, and style files. With that, the setting behaves as expected: a = 1 % 2 becomes a = 1%2. Fixes NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS crashes yapf when configured with % #1187.

  2. 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 in IndexError: 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 clean yapf: <file>:1:36: bad input instead 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 format dict[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.

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.
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.

NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS crashes yapf when configured with %

1 participant