Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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 theCPython 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:
Theast.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