Skip to main content

AST Unparser

https://badge.fury.io/py/astunparse.png https://travis-ci.org/simonpercivall/astunparse.png?branch=master https://readthedocs.org/projects/astunparse/badge/

An AST unparser for Python.

This is a factored out version of unparse found in the Python source distribution; under Demo/parser in Python 2 and under Tools/parser in Python 3.

Basic example:

import inspect
import ast
import astunparse

# get back the source code
astunparse.unparse(ast.parse(inspect.getsource(ast)))

# get a pretty-printed dump of the AST
astunparse.dump(ast.parse(inspect.getsource(ast)))

This library is single-source compatible with Python 2.6 through Python 3.5. It is authored by the Python core developers; I have simply merged the Python 2.7 and the Python 3.5 source and test suites, and added a wrapper. This factoring out is to provide a library implementation that supports both versions.

Added to this is a pretty-printing dump utility function.

The test suite both runs specific tests and also roundtrips much of the standard library.

Extensions and Alternatives

Similar projects include:

None of these roundtrip much of the standard library and fail several of the basic tests in the test_unparse test suite.

This library uses mature and core maintained code instead of trying to patch existing libraries. The unparse and the test_unparse modules are under the PSF license.

Extensions include:

  • typed-astunparse: extends astunparse to support type annotations.

Features

  • unparses Python AST.

  • pretty-prints AST.

Changelog

Here’s the recent changes to AST Unparser.

1.6.1 - 2018-10-03

  • Fix the roundtripping of very complex f-strings.

1.6.0 - 2018-09-30

  • Python 3.7 compatibility

1.5.0 - 2017-02-05

  • Python 3.6 compatibility

  • bugfix: correct argparser option type

1.4.0 - 2016-06-24

  • Support for the async keyword

  • Support for unparsing “Interactive” and “Expression” nodes

1.3.0 - 2016-01-17

  • Python 3.5 compatibility

1.2.0 - 2014-04-03

  • Python 2.6 through 3.4 compatibility

  • A new function dump is added to return a pretty-printed version of the AST. It’s also available when running python -m astunparse as the --dump argument.

1.1.0 - 2014-04-01

  • unparse will return the source code for an AST. It is pretty feature-complete, and round-trips the stdlib, and is compatible with Python 2.7 and Python 3.4.

    Running python -m astunparse will print the round-tripped source for any python files given as argument.

Release files for astunparse 1.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astunparse 1.6.1
File Size Uploaded
astunparse-1.6.1.tar.gz 13.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for astunparse 1.6.1
File Interpreter ABI Platform
astunparse-1.6.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 23.2 kB

Release files / astunparse-1.6.1.tar.gz

Download URL astunparse-1.6.1.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d27b16fb33dea0778c5a2c01801554eae0d3f8a8d6f604f15627589c3d6f11ca
BLAKE2b-256 checksum
How to use checksums
14533b080b8cff6a439d4371fcd02656b94b7325203b5c7f8cacc4789117b052
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

Release files / astunparse-1.6.1-py2.py3-none-any.whl

Download URL astunparse-1.6.1-py2.py3-none-any.whl
Size 10.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
dd4b33f553f5c3ec7a507a62c7111c65ae8e7ce330d0efc83d7f45fce3aad897
BLAKE2b-256 checksum
How to use checksums
0d9d1576217f67e7420f5945c15c6afd7045178c4850b148741bdbdbdabbf40e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

Release history Release notifications | RSS feed

1.6.3

2 release files

1.6.2

2 release files

This release

1.6.1 This release

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

1 release file

1.3.0

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

5 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page