Closed
Description
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--
>>> import ast
>>> 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}"}"}"}"}"}"'
>>> import ast
>>> tree = ast.parse(code)
>>> tree
<ast.Module object at 0x10992fed0>
>>> ast.unparse(tree)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 1777, in unparse
return unparser.visit(ast_obj)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 859, in visit
self.traverse(node)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 850, in traverse
super().visit(node)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 407, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 874, in visit_Module
self._write_docstring_and_traverse_body(node)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 867, in _write_docstring_and_traverse_body
self.traverse(node.body)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 848, in traverse
self.traverse(item)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 850, in traverse
super().visit(node)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 407, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 889, in visit_Expr
self.traverse(node.value)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 850, in traverse
super().visit(node)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 407, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 1240, in visit_JoinedStr
self._write_fstring_inner(value)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 1268, in _write_fstring_inner
self.visit_FormattedValue(node)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 1281, in visit_FormattedValue
raise ValueError(
ValueError: Unable to avoid backslash in f-string expression part