astpretty

3.0.0 · active · verified Mon Apr 13

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.

Warnings

Install

Imports

Quickstart

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

view raw JSON →