Skip to content

fix: preserve escaped newlines inside ordinary strings - #407

Merged
weibullguy merged 2 commits into
PyCQA:masterfrom
Dodothereal:fix/preserve-string-line-continuations
Nov 2, 2026
Merged

weibullguy merged 2 commits into
PyCQA:masterfrom
Dodothereal:fix/preserve-string-line-continuations

Conversation

@Dodothereal

@Dodothereal Dodothereal commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Prevent an escaped physical newline inside a quoted string from being mistaken for a backslash continuation between tokens.

On current master, even a file without docstrings can be corrupted:

print("first\
second")

Formatting duplicates the opening print( and produces invalid Python. Assignments can become duplicated chained assignments, with more copies added on every pass.

Use the existing original-row comparison to distinguish a string starting on the previous token's row from a genuine inter-token continuation. Existing explicit-continuation behavior is retained.

Add 52 regression cases covering ordinary quote styles, raw/bytes/Unicode prefixes, calls and returns, repeated continuations, concatenation, missing final newlines, parenthesized strings, shifted rows after docstring formatting, LF/CRLF, and default/--black modes. They check exact output, AST equivalence, and second-pass idempotence.

This was found during source inspection and synthetic testing, not from a production incident. It is distinct from #406's docstring-classification guard and from the already merged #378/#387 fixes.

Validation

Against upstream 82069d7f1e105bd86c69fd9ae0587ca00da3c312, on Linux / CPython 3.12.14:

  • Unchanged full suite: 686 passed, 3 skipped.
  • New tests on unchanged production code: 48 failures, 4 passing controls.
  • Fixed focused regressions: 52 passed.
  • Fixed full suite, including CLI tests: 738 passed, 3 skipped.
  • Pinned Black 26.3.1 and Ruff 0.15.11 pass both changed files. Black used explicit Python 3.10–3.12 targets under Python 3.12.
  • Independent Codex review checked the exact diff and hashes and repeated the full suite and quality checks.
  • Patch application and whitespace checks pass.

The complete tox/pre-commit aggregate and other interpreters/platforms' full test suites were not run locally. All seven hosted checks pass for b65883028ffa171b8f35a001993d177d16a04598.

AI assistance

Prepared with OpenAI Codex assistance for diagnosis, implementation, tests, and this description. A separate Codex agent independently reviewed and tested the exact final patch, with no actionable findings. The additional review was automated.

@Dodothereal
Dodothereal marked this pull request as ready for review October 30, 2026 09:32
@weibullguy
weibullguy merged commit ff13cf9 into PyCQA:master Nov 2, 2026
7 checks passed
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.

2 participants