Skip to content

Fix crash when f-string follows # fmt: off inside brackets#5097

Merged
cobaltt7 merged 3 commits intopsf:mainfrom
ParamChordiya:fix/fstring-fmtoff-crash
Apr 13, 2026
Merged

Fix crash when f-string follows # fmt: off inside brackets#5097
cobaltt7 merged 3 commits intopsf:mainfrom
ParamChordiya:fix/fstring-fmtoff-crash

Conversation

@ParamChordiya
Copy link
Copy Markdown
Contributor

Summary

Fixes #4511.

Black crashes with an AssertionError when formatting code that contains an f-string after a # fmt: off comment inside brackets:

(
# fmt: off
f"""
"""
# fmt: on
)

Error:

error: cannot format: {' ', 'r', 'f', 'o', ':', 'm', 't', '#', '\n'} is NOT a subset of {'U', 'r', 'f', 'u', 'F', 'R', 'b', 'B'}.

Root Cause

normalize_fmt_off runs before line generation and converts content between # fmt: off/# fmt: on into STANDALONE_COMMENT nodes. When this content is inside an f-string node, the f-string's children (FSTRING_START, FSTRING_MIDDLE, FSTRING_END) get replaced with a single STANDALONE_COMMENT leaf.

When visit_fstring is later called for this modified node, it calls fstring_tstring_to_string() which does str(node)[len(node.prefix):]. This produces a value like # fmt: off\nf"""\n""" — the comment text becomes part of the "string value", which fails the string prefix assertion.

Fix

Added a guard in visit_fstring (and visit_tstring) to detect when the node contains a STANDALONE_COMMENT child (indicating it was modified by normalize_fmt_off). In that case, fall back to visit_default instead of attempting the f-string-to-string conversion.

Test plan

  • Added regression tests in tests/data/cases/fmtonoff6.py for both triple-quoted and single-line f-strings with # fmt: off inside brackets
  • All 209 existing formatting tests pass
  • Verified the fix produces idempotent output

When an f-string appeared after a `# fmt: off` comment inside brackets,
Black would crash with an AssertionError about invalid string prefix
characters.

The root cause: `normalize_fmt_off` (which runs before line generation)
converts content between `# fmt: off` and `# fmt: on` into
STANDALONE_COMMENT nodes. When this content is inside an f-string node,
the f-string's children get replaced with a STANDALONE_COMMENT leaf.
However, `visit_fstring` still tried to call `fstring_tstring_to_string`
on the modified node, producing a "string" value like
`# fmt: off\nf"""\n"""` which fails the string prefix assertion.

The fix adds a guard in `visit_fstring` (and `visit_tstring`) to detect
when the node has been modified by `normalize_fmt_off` and fall back to
`visit_default` instead of attempting the f-string-to-string conversion.

Fixes psf#4511.
Comment thread tests/data/cases/fmtonoff6.py
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

diff-shades results comparing this PR (67adc97) to main (06e496a):

--preview style: no changes

--stable style: no changes


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

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

# Same as above but unindented inside brackets.
(
# fmt: off
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the comments wouldn't move. Would that be a lot of extra work? If so it's fine like this.

Please also add a changelog entry

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment movement (re-indenting # fmt: off/# fmt: on to match bracket depth) is existing Black behavior for any comment inside brackets — it's not introduced by this PR. Changing that would require modifying the general comment normalization/line generation logic, which feels out of scope for this crash fix. Happy to look into it as a follow-up if you think it's worth pursuing.

Added a changelog entry under Stable style in 67adc97.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Thank you!

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cobaltt7 cobaltt7 merged commit fc33c25 into psf:main Apr 13, 2026
52 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.

Error formatting f-string with # fmt: off inside a pair of brackets

3 participants