Description
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
- Create a file
broken_fstring.py
:
# broken_fstring.py
def foo():
# This *should* be a SyntaxError:
x = {}
yield f"Hello, {x.get("inner")}!"
- 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 '('
- 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 commentedon May 16, 2025
ericvsmith commentedon May 16, 2025
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 commentedon May 17, 2025
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.