Closed
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Is this desired behavior?
$ ./python -m ast
f'{""" # this is part of the string, it shouldn't be stripped out
"""=}'
Module(
body=[
Expr(
value=JoinedStr(
values=[
Constant(value='""" \n"""='),
FormattedValue(
value=Constant(value=" # this is part of the string, it shouldn't be stripped out\n"),
conversion=114)]))])
$ ./python -m ast
t'{""" # this is part of the string, it shouldn't be stripped out
"""=}'
Module(
body=[
Expr(
value=TemplateStr(
values=[
Constant(value='""" \n"""='),
Interpolation(
value=Constant(value=" # this is part of the string, it shouldn't be stripped out\n"),
str='""" \n"""',
conversion=114)]))])
Even if they are not actually multiline:
$ python -m ast
t'{" # nooo "=}'
Module(
body=[
Expr(
value=TemplateStr(
values=[
Constant(value='" '),
Interpolation(
value=Constant(value=' # nooo '),
str='"',
conversion=114)]))])
Seems to go back to 3.12
CPython versions tested on:
3.14, CPython main branch
Operating systems tested on:
No response
Linked PRs
Metadata
Metadata
Assignees
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
JelleZijlstra commentedon Jun 5, 2025
Yeah this is clearly wrong. cc @pablogsal @lysnikolaou
[-]Parts of strings that look like comments being stripped out of multiline strings[/-][+]Parts of strings that look like comments being stripped out of nested strings with debug specifier[/+]JelleZijlstra commentedon Jun 5, 2025
This is actually a regression in 3.12, likely due to PEP-701:
Removing the release blocker label as it's old.
tom-pytel commentedon Jun 5, 2025
Not just debug specifier, note the
.strfield:Could this be problematic?
lysnikolaou commentedon Jun 5, 2025
Oof, yeah, this was probably introduced in #112284. We'll need to fix it, but the fix is not exactly straight-forward. I'll take a deeper look tomorrow.
tom-pytel commentedon Jun 5, 2025
Not sure if this would apply in the context of the parser but worked for me before I realized it wasn't matching up.
In range of interest exclude all lines which have multiline strings which do not end on the line.
For each remaining line store maximum end column of expressions that end on that line and restrict comment checks to past this column.
2 remaining items
pythongh-135148: Correctly handle f/t strings with comments and debug…
pythongh-135148: Correctly handle f/t strings with comments and debug…
pythongh-135148: Correctly handle f/t strings with comments and debug…
gh-135148: Correctly handle f/t strings with comments and debug expre…
pythongh-135148: Correctly handle f/t strings with comments and debug…
[3.14] gh-135148: Correctly handle f/t strings with comments and debu…
[3.13] gh-135148: Correctly handle f/t strings with comments and debu…
lysnikolaou commentedon Jul 21, 2025
All merged now.
pythongh-135148: Correctly handle f/t strings with comments and debug…
pythongh-135148: Correctly handle f/t strings with comments and debug…
[3.14] pythongh-135148: Correctly handle f/t strings with comments an…