Registry / testing / astpretty

astpretty

JSON →
library3.0.0pypypi✓ verified 22d ago

astpretty is a Python library designed to pretty-print the Abstract Syntax Tree (AST) generated by Python's standard library `ast.parse` function. It offers a more readable, indented, and detailed representation of AST nodes compared to the built-in `ast.dump`, making it useful for debugging and understanding code structure. The current version is 3.0.0, released in May 2022, requiring Python 3.8+. It's a stable, focused utility with infrequent but significant updates.

pip install astpretty
INSTALL
IMPORT
SIG · ASTPRETTY
A
astpretty
testingpythonv3.0.0
Install
1.5s avg
Import
17ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.0 · 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.103.95 runs
installs and imports cleanly · install 0.0s · import 0.018s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.016s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

pprint
from astpretty import pprint
import astpretty astpretty.pprint(node)

This example demonstrates parsing a simple Python code snippet into an AST using the standard `ast` module and then using `astpretty.pprint` to display its structured, human-readable representation. You can also use `astpretty.pformat` to get the pretty-printed string instead of printing directly to stdout.

import ast import astpretty code_snippet = """ def greet(name): print(f'Hello, {name}!') """ # Parse the code into an AST node parsed_ast = ast.parse(code_snippet) # Pretty print the entire AST astpretty.pprint(parsed_ast) # Or pretty print a specific node, e.g., the function definition # astpretty.pprint(parsed_ast.body[0], show_offsets=False, indent=' ') # Example with options
astpretty --version
Debug
Known issues
breakingPython 2 support was dropped with `astpretty` version 2.0.0. Version 3.0.0 further increased the minimum Python requirement to 3.8. Older Python 2 or early Python 3 projects will need to use an older `astpretty` version or upgrade their Python interpreter.
fix
Upgrade your Python environment to 3.8 or newer. For Python 2, use `astpretty < 2.0.0` (not recommended for new development).
affects: >=2.0.0
gotcha`astpretty.pprint` provides a highly detailed, multi-line, indented output, including `lineno` and `col_offset` by default. This is significantly more verbose than the standard library's `ast.dump`, which outputs a single-line representation. Users accustomed to `ast.dump` might find `astpretty`'s default output overwhelming if they only need a compact string.
fix
For less verbosity, use `astpretty.pprint(node, show_offsets=False)`. For Python 3.9+, consider `ast.dump(node, indent=...)` if a simpler, indented string is sufficient. `astpretty` generally offers more detailed AST information.
affects: All versions
gotcha`astpretty` operates on an existing AST node object, not directly on source code strings. You must first parse your code into an AST object using `ast.parse` from the Python standard library before passing it to `astpretty.pprint` or `astpretty.pformat`.
fix
Always use `import ast` and `ast.parse(your_code_string)` to obtain an AST node, then pass that node to `astpretty` functions.
affects: All versions
Upgrade
Version history
3.0.0latest on PyPI · released May 16, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
astpretty — pip install astpretty · libregistry