prune-source0.1.6
Published
Prune Python function bodies before compiling abstract syntax trees.
pip install prune-source
Package Downloads
Authors
Requires Python
>=3.11
Dependencies
No dependencies
prune-source
prune-source is a small Rust/Ruff accelerator for static API analysis. It lexes Python source and
removes function implementation suites that an API visitor does not need, allowing CPython to
construct and walk a much smaller abstract syntax tree. It does not build an intermediate Ruff AST.
pip install prune-source
import ast
from prune_source import prune_source
source = "def answer() -> int:\n return 42\n"
pruned = prune_source(source)
tree = compile(
source if pruned is None else pruned,
"example.py",
"exec",
ast.PyCF_ONLY_AST,
optimize=1,
)
The pruner preserves function signatures, decorators, docstrings, class bodies, and class
__init__ implementations. Other function suites are replaced by pass while physical line
layout is retained. Removed multiline content is collapsed to empty lines rather than padded with
spaces; for indented suites, the pass remains on the original final implementation line so
definition end-line metadata stays stable. Nested function suites inside __init__ are pruned as
well.
The input is expected to be valid for the Python interpreter that will compile it. The lexer does
not perform grammar or version validation. prune_source returns None when there is nothing to
prune; callers should compile their original source in that case.
The pruner uses Ruff's published parser crate to read token ranges and flags without constructing a syntax tree. Ruff's Rust API is unstable, so its component crates are pinned to the same version.
Building from source requires Rust 1.96 or newer.