Skip to main content

An AST unparser for Python

Project description

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.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-backwards-astunparse-1.5.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

py_backwards_astunparse-1.5.0-py2.py3-none-any.whl (12.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file py-backwards-astunparse-1.5.0.tar.gz.

File metadata

File hashes

Hashes for py-backwards-astunparse-1.5.0.tar.gz
Algorithm Hash digest
SHA256 7cebc29a808b29b49baf1dd626e8da5de0d1dc555bb82b5f0575d205fec0878d
MD5 fe32b47126b582255efefe17e2eab124
BLAKE2b-256 006f5e153906a9cc25f9a5ecfa25b11bb3e51720378f427fe05dee73f9a9259c

See more details on using hashes here.

File details

Details for the file py_backwards_astunparse-1.5.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for py_backwards_astunparse-1.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 874da992f7807e26f94dab4ce72dd4cfbc2da77b04c0d97337cdf0fd0430c8f6
MD5 ce202019e55f1e9e62271625fda0c4be
BLAKE2b-256 1dd8411c5ad4fe3b97b0364359d9ba0d33b33e874f8d6bdf1d229d0b0fa1f99b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page