An AST unparser for Python
Project description
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.
Documentation: http://astunparse.rtfd.org.
Features
unparses Python AST.
pretty-prints AST.
Changelog
Here’s the recent changes to AST Unparser.
1.6.3 - 2019-12-22
Add full support for Python 3.8
1.6.2 - 2019-01-19
Add support for the Constant node in Python 3.8
Add tests to the sdist
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file astunparse-fixed-1.7.0.tar.gz
.
File metadata
- Download URL: astunparse-fixed-1.7.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32e5028665709e83bb5be9396d0a3f2dd06d566e5702c62acf4ad49df3ab1a8e |
|
MD5 | 148170c493a57654266da8ad5e1c6da9 |
|
BLAKE2b-256 | f25a0bd311457b5132144119e6336b3907bb9af14e878e4451c5f4b1e4f53ea2 |
File details
Details for the file astunparse_fixed-1.7.0-py2.py3-none-any.whl
.
File metadata
- Download URL: astunparse_fixed-1.7.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee0c91763b1faa2be6e8d9bddc512d552d983ac40315df4c6839a692cee2e805 |
|
MD5 | 8b659ab4e6a2e0395c7bac8bf33753cd |
|
BLAKE2b-256 | b49200d2d84ec586418204542162faff185cf2b1cca699dd582c98bba573896a |