Fix crash when f-string follows # fmt: off inside brackets#5097
Merged
Fix crash when f-string follows # fmt: off inside brackets#5097
Conversation
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.
cobaltt7
reviewed
Apr 12, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
cobaltt7
reviewed
Apr 13, 2026
|
|
||
| # Same as above but unindented inside brackets. | ||
| ( | ||
| # fmt: off |
Collaborator
There was a problem hiding this comment.
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
Contributor
Author
There was a problem hiding this comment.
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.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
JelleZijlstra
approved these changes
Apr 13, 2026
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.
Summary
Fixes #4511.
Black crashes with an
AssertionErrorwhen formatting code that contains an f-string after a# fmt: offcomment inside brackets:Error:
Root Cause
normalize_fmt_offruns before line generation and converts content between# fmt: off/# fmt: onintoSTANDALONE_COMMENTnodes. 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_fstringis later called for this modified node, it callsfstring_tstring_to_string()which doesstr(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(andvisit_tstring) to detect when the node contains a STANDALONE_COMMENT child (indicating it was modified bynormalize_fmt_off). In that case, fall back tovisit_defaultinstead of attempting the f-string-to-string conversion.Test plan
tests/data/cases/fmtonoff6.pyfor both triple-quoted and single-line f-strings with# fmt: offinside brackets