Skip to content

Commit

Permalink
bpo-38870: Expose a function to unparse an ast object in the ast modu…
Browse files Browse the repository at this point in the history
…le (GH-17302)

Add ast.unparse() as a function in the ast module that can be used to unparse an
ast.AST object and produce a string with code that would produce an equivalent ast.AST
object when parsed.
  • Loading branch information
pablogsal authored Nov 24, 2019
1 parent 6bf644e commit 27fc3b6
Show file tree
Hide file tree
Showing 6 changed files with 772 additions and 751 deletions.
13 changes: 13 additions & 0 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ and classes for traversing abstract syntax trees:
Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.


.. function:: unparse(ast_obj)

Unparse an :class:`ast.AST` object and generate a string with code
that would produce an equivalent :class:`ast.AST` object if parsed
back with :func:`ast.parse`.

.. warning::
The produced code string will not necesarily be equal to the original

This comment has been minimized.

Copy link
@tirkarthi

tirkarthi Nov 25, 2019

Member

Minor typo here : necesarily -> necessarily

code that generated the :class:`ast.AST` object.

.. versionadded:: 3.9


.. function:: literal_eval(node_or_string)

Safely evaluate an expression node or a string containing a Python literal or
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ Added the *indent* option to :func:`~ast.dump` which allows it to produce a
multiline indented output.
(Contributed by Serhiy Storchaka in :issue:`37995`.)

Added the :func:`ast.unparse` as a function in the :mod:`ast` module that can
be used to unparse an :class:`ast.AST` object and produce a string with code
that would produce an equivalent :class:`ast.AST` object when parsed.
(Contributed by Pablo Galindo and Batuhan Taskaya in :issue:`38870`.)

asyncio
-------

Expand Down
Loading

0 comments on commit 27fc3b6

Please sign in to comment.