Skip to content

ast.unparse doesn't observe the new PEP701 string delimiter rules #108469

Description

@tonybaloney

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker,
    and am confident this bug has not been reported before

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.12.0b4 (v3.12.0b4:97a6a41816, Jul 11 2023, 11:19:02) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

A clear and concise description of the bug:

The ast.unparse Unparser doesn't seem to respect PEP701, for example, if you use double quotes in an f-string then unparse the AST it will use a backslash--

>>>importast>>>code='f" something { my_dict["key"] } something else "'>>>tree=ast.parse(code)
>>>ast.unparse(tree)
'f" something {my_dict[\'key\']} something else "'

Furthermore, if you use the nested f-string example in the PEP, it crashes completely when unparsing the AST

>>>f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"'2'>>>code='f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"'>>>importast>>>tree=ast.parse(code)
>>>tree<ast.Moduleobjectat0x10992fed0>>>>ast.unparse(tree)
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line1777, inunparsereturnunparser.visit(ast_obj)
^^^^^^^^^^^^^^^^^^^^^^^File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line859, invisitself.traverse(node)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line850, intraversesuper().visit(node)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line407, invisitreturnvisitor(node)
^^^^^^^^^^^^^File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line874, invisit_Moduleself._write_docstring_and_traverse_body(node)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line867, in_write_docstring_and_traverse_bodyself.traverse(node.body)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line848, intraverseself.traverse(item)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line850, intraversesuper().visit(node)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line407, invisitreturnvisitor(node)
^^^^^^^^^^^^^File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line889, invisit_Exprself.traverse(node.value)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line850, intraversesuper().visit(node)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line407, invisitreturnvisitor(node)
^^^^^^^^^^^^^File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line1240, invisit_JoinedStrself._write_fstring_inner(value)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line1268, in_write_fstring_innerself.visit_FormattedValue(node)
File"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line1281, invisit_FormattedValueraiseValueError(
ValueError: Unabletoavoidbackslashinf-stringexpressionpart

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions