Skip to content

Alternate nested quotes inside format spec interpolations - #28259

Merged
MichaReiser merged 4 commits into
astral-sh:mainfrom
qatcod:fstring-format-spec-nested-quotes
Sep 7, 2026
Merged

MichaReiser merged 4 commits into
astral-sh:mainfrom
qatcod:fstring-format-spec-nested-quotes

Conversation

@qatcod

@qatcod qatcod commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #28218.

When Ruff formats an f-string, it normally alternates the inner quotes so they don't conflict with the outer ones. For example, a normal interpolation like f'x {d["k"]} y' is formatted as f"x {d['k']} y".

However, this didn't work correctly for interpolations inside a format spec. For example, f'x {v:,.{d["n"]}f} y' was left with {d["n"]} using the same quote character as the outer f-string.

Reusing the outer quote character inside an f-string is only valid starting with Python 3.12, thanks to PEP 701. With target-version = "py311", this meant Ruff's formatter could produce code that its own checker would reject with:

Cannot reuse outer quote character in f-strings on Python 3.11 (syntax was added in Python 3.12)

The issue was caused by how Ruff tracks nesting inside interpolated strings. A field inside a format spec was incorrectly treated as an additional nesting level, even though it's still part of the same f-string and doesn't introduce another string literal.

That incorrect nesting level caused the formatter to preserve the existing quotes for Python versions before 3.12. In this case, preserving them resulted in invalid Python 3.11 syntax.

The fix is to only increase the nesting level when entering another string literal. Entering a format spec does not count as deeper string nesting, so fields inside format specs now stay at the current nesting level.

A replacement field inside a format spec belongs to the same string as the
field enclosing it, but it was being treated as a further level of nesting.
Before Python 3.12 that made the formatter preserve the inner quotes and
reuse the outer quote character, which is a syntax error the parser rejects.

Only entering another string literal is a further level of nesting, so keep
the state unchanged for format spec fields.
@qatcod
qatcod requested a review from MichaReiser as a code owner September 2, 2026 19:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-02T19:50:50.196090Z c96346d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@astral-sh-bot

astral-sh-bot Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

ruff-ecosystem results

Formatter (stable)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

sphinx-doc/sphinx (error)

ruff format --no-preview --exclude tests/roots/test-pycode/cp_1251_coded.py

Failed to commit: fatal: failed to encode 'tests/roots/test-root/wrongenc.inc' from iso-8859 to UTF-8

Formatter (preview)

✅ ecosystem check detected no format changes.

qatcod and others added 2 commits September 3, 2026 01:20
cargo hawk flags them as unnecessarily visible: both are only used inside
the module that defines them.
Format-spec interpolations belong to the containing f-string or t-string. Advance the nesting depth when entering a string, and retain each interpolation’s own layout context without introducing format-spec markers.

Regression: astral-sh#28218
@MichaReiser MichaReiser added bug An issue describing something that isn't working, or a PR that fixes a bug formatter Related to the Ruff formatter labels Sep 7, 2026
@MichaReiser

MichaReiser commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Thank you. I made a few smaller changes, mainly to avoid introducing more state. The implementation now has an explicit enter_string method that both f-string and t-string formatting calls. This removes the need to distinguish between in and outside format specs.

@MichaReiser
MichaReiser merged commit b3fb43a into astral-sh:main Sep 7, 2026
80 of 82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug formatter Related to the Ruff formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter does not apply nested-string-quote-style alternation inside format-spec interpolations

2 participants