$ show asottile/astpretty
astpretty is a Python library and CLI tool that provides a pretty-print alternative to the standard library's `ast.dump`. It formats Python AST nodes (from `ast.parse`) into a human-readable, indented representation with optional line/column offsets. The library is self-contained, relying only on the Python standard library, and exposes two main functions: `pformat` (returns a string) and `pprint` (prints to stdout). The CLI entry point reads a Python file, parses it, and prints the AST. The code is well-typed, uses modern Python features (3.11+), and includes comprehensive tests covering edge cases like type comments and version-specific AST changes. It is a polished, focused utility for developers working with Python's AST.
The LLM identified 1 files worth highlighting for reuse:
The core module implementing the AST pretty-printer. It provides `pformat` and `pprint` functions that recursively traverse AST nodes, handling leaf nodes (simple values) and nested nodes with proper indentation. Key design decisions include: using a context manager for indentation state, separating leaf detection (`_is_leaf`) from formatting, supporting both string and integer indent specifications, and optionally including line/column offsets. The module is self-contained (no external dependencies), fully typed, and demonstrates clean recursive formatting logic that could be adapted for other tree-like data structures.