Registry / serialization / astunparse

astunparse

JSON →
library1.6.3pypypi✓ verified 29d ago

astunparse is a Python library that provides an Abstract Syntax Tree (AST) unparser, allowing conversion of ASTs back into Python source code. It is a standalone, factored-out version of the `unparse` utility originally found within the Python source distribution itself. In addition to unparsing, it offers a `dump` function for pretty-printing AST structures. The current version is 1.6.3, released in December 2019, and it has a low-frequency release cadence, primarily updating for compatibility with newer Python AST changes.

pip install astunparse
INSTALL
IMPORT
SIG · ASTUNPARSE
A
astunparse
serializationpythonv1.6.3
Install
1.7s avg
Import
18ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.020s · 18MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.016s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

unparse
✓ from astunparse import unparse
✗ import astunparse
dump
✓ from astunparse import dump
Printer
✓ from astunparse import Printer

This quickstart demonstrates how to parse Python source code into an AST using the built-in `ast` module, then use `astunparse.unparse` to convert the AST back into source code, and `astunparse.dump` to get a pretty-printed representation of the AST.

import ast import inspect import astunparse # Get the AST of a simple function def example_func(x, y): return x + y source_code = inspect.getsource(example_func) parsed_ast = ast.parse(source_code) # Unparse the AST back to source code unparsed_code = astunparse.unparse(parsed_ast) print('--- Unparsed Code ---') print(unparsed_code) # Pretty-print the AST dumped_ast = astunparse.dump(parsed_ast) print('\n--- Dumped AST ---') print(dumped_ast)
Debug
Known issues
gotchaPython 3.9+ includes `ast.unparse` natively. The standalone `astunparse` library is primarily useful for projects targeting Python versions prior to 3.9 or when specific behaviors of this library are preferred over the built-in one. For newer Python versions, `ast.unparse` is generally the recommended approach.
fix
For Python 3.9 and newer, prefer `ast.unparse(node)` directly. For older versions or specific `astunparse` features, continue using the library.
affects: <3.9 for standalone usage relevance
breakingWhile `astunparse` aims for broad compatibility (Python 2.6 through 3.8 as per its last update), the underlying Python AST structure can change significantly between major Python versions. For example, Python 3.12 introduced changes (PEP 695) to AST node attributes, which could affect code relying on specific AST node structures or the exact output for constructs like f-strings. This means strict round-trip compatibility across different Python versions cannot always be guaranteed by any AST unparser.
fix
Test your unparsing logic thoroughly against target Python versions. When targeting newer Python versions, consider if the built-in `ast.unparse` might be more up-to-date with recent AST changes than the standalone library.
affects: All versions, especially when moving between major Python releases (e.g., Python 3.8 to 3.9+, or 3.11 to 3.12+).
gotcha`astunparse` (and the standard `ast` module it derives from) does not preserve or unparse PEP 484 type comments. If your project's code includes type annotations and you need them preserved after unparsing, `astunparse` will discard them.
fix
For use cases requiring the preservation of type comments, consider alternative libraries like `typed-astunparse`, which are built to work with `typed-ast` (an AST variant that includes type comment nodes).
affects: All versions
Upgrade
Version history
1.6.3latest on PyPI · released Dec 22, 2019
Audit
Dependencies
sixrequiredCompatibility layer for Python 2 and 3, used internally for dual-version support.
Agent activity
33 hits · last 30 days
node
28
OpenAI (training)
1
Resources