Skip to content

bpo-38870: Expose a function to unparse an ast object in the ast module #17302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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