Skip to content

fix: handle apostrophes, backslashes and block strings in operation strings - #477

Merged
DamianCzajkowski merged 2 commits into
mainfrom
fix/operation-string-quotes
Nov 2, 2026
Merged

DamianCzajkowski merged 2 commits into
mainfrom
fix/operation-string-quotes

Conversation

@DamianCzajkowski

Copy link
Copy Markdown
Collaborator

Fixes #423, fixes #288. Supersedes #454.

Problem

format_multiline_strings turns the implicitly concatenated literals produced by ast.unparse into """ blocks. It stripped every ' and copied the escaped source into the block verbatim. As a result:

  • an operation containing an apostrophe ("Foo's bar", WHERE type = 'Product') produced invalid Python and generation crashed in ruff format;
  • a GraphQL block string ("""…""") inside an operation closed the Python block early;
  • a backslash escape ("C:\\path") was decoded once more by the non-raw """ block, so the client sent invalid GraphQL (\p) at runtime.

Changes

  • Rebuild the real string value with ast.literal_eval and render a """ block only when that is lossless: no embedded """, backslash or \r. Otherwise keep the escaped, concatenated form, which is still valid Python.
  • Match runs of adjacent literals with a regex that understands escapes and never crosses a newline. This keeps consecutive X_GQL = … assignments on their own lines, which is the case fix: handle escaped single quotes when formatting multiline strings #454 broke in client_with_operations.

Tests

  • Unit tests for apostrophes, embedded block strings, backslashes, consecutive assignments, and the escaped single quote case from fix: handle escaped single quotes when formatting multiline strings #454 (credited via Co-authored-by).
  • Four of the new tests fail on the old implementation.
  • End-to-end check: generated a client for operations with ', "Foo's bar" (Codegen fails when a GQL string contains ' #288), "C:\\path\\new", \t and a plain string, then compared each operation string the client sends with the source. All five match; on main, generation crashes, and the backslash case is broken there even without apostrophes.

hatch run lint passes, and hatch test -a: 1055 passed on Python 3.10–3.14.

DamianCzajkowski and others added 2 commits November 2, 2026 20:24
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>
@coderabbitai

coderabbitai Bot commented Nov 2, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 080a9ffa-c25b-454e-a14f-202e0a952476

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@DamianCzajkowski
DamianCzajkowski merged commit 673da28 into main Nov 2, 2026
6 checks passed
@DamianCzajkowski
DamianCzajkowski deleted the fix/operation-string-quotes branch November 2, 2026 19:32
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.

Client generation doesn't handle multiline strings in the queries file Codegen fails when a GQL string contains '

1 participant