Skip to content

Fix crash on backslash-only lines in code Black leaves as is - #5681

Merged
cobaltt7 merged 2 commits into
psf:mainfrom
haeganm:fix-lone-backslash-in-verbatim
Nov 12, 2026
Merged

cobaltt7 merged 2 commits into
psf:mainfrom
haeganm:fix-lone-backslash-in-verbatim

Conversation

@haeganm

@haeganm haeganm commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Description

a line that's only a backslash, right after another backslash continuation, makes Black crash on code it's supposed to leave alone:

# fmt: off
x = [1,
     2] + \
    \
    [3]
# fmt: on

that gives INTERNAL ERROR: Black produced invalid code. same thing on a # fmt: skip line and on lines outside --line-ranges. in an f-string field with =, or any t-string field, the field's source is part of the string, so it's "not equivalent to the source" instead, and with --fast Black just writes the changed string. CPython's own Lib/test/test_tstring.py hits it. it's a regression from 24.8.0, 24.4.2 handles all of these.

since #4343, transform_whitespace only turns the first backslash-newline into an NL token. the second one gets skipped, so the driver puts a plain newline in the prefix where the backslash was. regular code gets its whitespace redone so it doesn't matter there, but fmt: off/skip code and f-string fields are written straight from the prefix. now a backslash line that follows a backslash NL becomes an NL token too, so it stays in the prefix. list_comments still counts it as an empty line, so comments land where they did before.

output only changes where Black used to crash or drop the backslash, so already formatted files come out the same.

a lone backslash right after a regular newline still turns into a blank line, same as before. handling that too would also fix f"""{value / \ / =}""", but it changes blank lines after comments, so I left it out.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

A line holding only a backslash, right after another backslash
continuation, wasn't made into an NL token, so the driver put a plain
newline in the prefix where it was. fmt: off/skip code, lines outside
--line-ranges and f-/t-string fields are written back from the prefix,
so the backslash got dropped and the output was invalid, or a different
string in an f-string field with `=` or a t-string field. Broken since
24.8.0 (psf#4343).

Make it an NL token when it follows a backslash NL so it stays in the
prefix. list_comments still counts it as an empty line, so comments
don't move.
@haeganm
haeganm force-pushed the fix-lone-backslash-in-verbatim branch from a231434 to 14d18c0 Compare November 10, 2026 03:11
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (a1c2742) to main (bb56667):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@cobaltt7

Copy link
Copy Markdown
Collaborator

Thanks!

@cobaltt7
cobaltt7 merged commit 741cab1 into psf:main Nov 12, 2026
54 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