Converting AST to text

Issue #21 new
Alex Hall created an issue

The section Going backwards says:

Python itself doesn’t provide a way to turn a compiled code object into an AST, or an AST into a string of code.

Python 3.9 added ast.unparse which converts an AST node to a string:

>>> ast.unparse(ast.parse("x = 1 + 2"))
'\nx = 1 + 2'

Python 3.8 added get_source_segment which gives the original source code of a specific node using line numbers and column offsets and the full source code:

>>> source = "x = 1 + 2"
>>> tree = ast.parse(source)
>>> tree.body[0].value
<ast.BinOp object at 0x10c8bf160>
>>> ast.get_source_segment(source, tree.body[0].value)
'1 + 2'

asttokens is a great project which can do the work of get_source_segment for older versions, back to Python 2.7. I have used it for many projects of my own.

Comments (1)

  1. Thomas Kluyver repo owner

    We should definitely mention unparse once it’s nearing release.

    I’m open to also pointing people to get_source_segment and asttokens, but they should be clearly distinguished. If I understand correctly, they’re looking back at code that was parsed, so you can’t use them to turn a generated or modified AST into code.

  2. Log in to comment