Repository navigation
fix: handle apostrophes, backslashes and block strings in operation strings - #477
Merged
Merged
Conversation
format_multiline_strings stripped every apostrophe and wrapped content in
"""...""" unconditionally, so operations containing an apostrophe or a
GraphQL block string (""") produced invalid Python and crashed the formatter.
Reconstruct the real string via ast.literal_eval and only convert to a
triple-quoted block when it is lossless and safe (no embedded """, backslash
or carriage return); otherwise keep the escaped, implicitly-concatenated form.
Fixes #423, #288
The escaped single quote case is adapted from #454. Co-authored-by: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
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 #423, fixes #288. Supersedes #454.
Problem
format_multiline_stringsturns the implicitly concatenated literals produced byast.unparseinto"""blocks. It stripped every'and copied the escaped source into the block verbatim. As a result:"Foo's bar",WHERE type = 'Product') produced invalid Python and generation crashed inruff format;"""…""") inside an operation closed the Python block early;"C:\\path") was decoded once more by the non-raw"""block, so the client sent invalid GraphQL (\p) at runtime.Changes
ast.literal_evaland render a"""block only when that is lossless: no embedded""", backslash or\r. Otherwise keep the escaped, concatenated form, which is still valid Python.X_GQL = …assignments on their own lines, which is the case fix: handle escaped single quotes when formatting multiline strings #454 broke inclient_with_operations.Tests
Co-authored-by).',"Foo's bar"(Codegen fails when a GQL string contains'#288),"C:\\path\\new",\tand a plain string, then compared each operation string the client sends with the source. All five match; onmain, generation crashes, and the backslash case is broken there even without apostrophes.hatch run lintpasses, andhatch test -a: 1055 passed on Python 3.10–3.14.