Skip to content

f string with outer and inner double quotes produce no syntax issue in python 3.13, but it does on previous versions. #134111

Not planned
@orlando-fbn

Description

@orlando-fbn

Bug report

Bug description:

Bug Report: f-string Regression in Python 3.13 Parser

Python Versions

  • 3.11 – correctly rejects inner/outer matching‐quote f-strings
  • 3.13 – silently accepts invalid f-string syntax

Issue Type

Parser / grammar regression


Description

In Python 3.11 and earlier, an f-string whose inner quotes match the outer quotes (for example f"…{x.get("inner")}…") raises a SyntaxError. In Python 3.13, however, the same invalid code is accepted at compile time, leading to silent misparsing.


Steps to Reproduce

  1. Create a file broken_fstring.py:
# broken_fstring.py

def foo():
    # This *should* be a SyntaxError:
    x = {}
    yield f"Hello, {x.get("inner")}!"
  1. Under Python 3.11:
    python3.11 -m py_compile broken_fstring.py

Expected:

  File "reportlib/report_generator/python_bug.py", line 7
    yield f"Hello, {x.get("inner")}!"
                           ^^^^^
SyntaxError: f-string: unmatched '('
  1. Under Python 3.13:
    python3.13 -m py_compile broken_fstring.py

Actual: no error, exit code 0.


Expected Behavior

The compiler should reject the invalid f-string syntax in all supported versions, consistent with PEP 498.


Minimal Test Case

# Create the test file
cat > broken_fstring.py << 'EOF'
def foo():
    x = {}
    yield f"Hello, {x.get("inner")}!"
EOF

Under 3.11:

python3.11 -m py_compile broken_fstring.py # raises SyntaxError

Under 3.13:

python3.13 -m py_compile broken_fstring.py # silently succeeds

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Activity

Eclips4

Eclips4 commented on May 16, 2025

@Eclips4
Member
No description provided.
removed
type-bugAn unexpected behavior, bug, or error
on May 16, 2025
ericvsmith

ericvsmith commented on May 16, 2025

@ericvsmith
Member

In general, PEPs are historical documents. They shouldn't be relied on for the description of Python's behavior. Even current PEPs! Instead, refer to the documentation of the feature in question.

That said, I don't know if the docs have been updated to reflect this change.

ned-deily

ned-deily commented on May 17, 2025

@ned-deily
Member

https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals

Changed in version 3.12: Prior to Python 3.12, reuse of the same quoting type of the outer f-string inside a replacement field was not possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      f string with outer and inner double quotes produce no syntax issue in python 3.13, but it does on previous versions. · Issue #134111 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant