Skip to content

fix: an f-string field may hold an f-string in the same quote - #241

Merged
jonruttan merged 1 commit into
mainfrom
fix/fstring-nested
Nov 19, 2026
Merged

jonruttan merged 1 commit into
mainfrom
fix/fstring-nested

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

Conformance: string_fstring_nested_py312.

Python 3.12 (PEP 701) lets a replacement field hold any expression, a string in the f-string's own quote included: f"a{f"b{x}c"}d" is one literal, nested to any depth.

  • tokens.x: the f-string body is a stack of frames (literal text, field, brace in a field, plain string, escape) instead of a run to the first quote. Prefixes f, rf and fr in either case use it; {{ stays the literal brace.
  • The engine reuses one integer object for every character (x-token.c, arg_chr). The body copies the code before a frame keeps it, or a kept quote would already be the next character.
  • parse.x: the field scanner skips a nested f-string whole.
  • Unclosed fields still report CPython's error: a field holding the outer quote that runs to end of input takes the literal to the end, so f"{(1" gives f-string: expecting '}' rather than invalid syntax.
  • tests: 38-format-fstrings-4 gains a nesting case (same quote, depth 3, escapes, {{/}}, raw).

The full suite passes locally on x-lang v0.57.0.

🤖 Generated with Claude Code

PEP 701 (Python 3.12): a replacement field holds any expression, so
f"a{f"b{x}c"}d" is one literal. The f-string body in tokens.x is now a
stack of frames (literal text, field, brace, plain string, escape)
instead of the plain body's run to the first quote, and the field
scanner in parse.x skips a nested f-string whole.

The tokenizer copies each character before a frame keeps it: the engine
hands every state one integer object, rewritten per character.

A field holding the outer quote that runs to the end of input takes the
literal to the end, so f"{(1" still reports "f-string: expecting '}'".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit e29c457 into main Nov 19, 2026
12 checks passed
@jonruttan
jonruttan deleted the fix/fstring-nested branch November 19, 2026 20:45
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.

1 participant