Skip to main content

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.

Ruff's internal parser crate is vendored because its public lexer API does not expose token ranges. The vendored copy has exactly two source changes, making the range and flag accessors public; see vendor/ruff_python_parser/UPSTREAM.md.

Building from source requires Rust 1.96 or newer.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prune_source-0.1.0.tar.gz (153.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prune_source-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl (226.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.34+ x86-64

File details

Details for the file prune_source-0.1.0.tar.gz.

File metadata

  • Download URL: prune_source-0.1.0.tar.gz
  • Upload date:
  • Size: 153.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"EndeavourOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for prune_source-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4117e80a146212ba816e83c665f3951716457d9bed94d7cb5dbaf33bff5b08e1
MD5 836a30d306f8f96f5d03b263dfe1ff10
BLAKE2b-256 ef8a9bb344136aa803e5f987746a4642aedf6c7cac6ba316d9b5bb45f22ffeaf

See more details on using hashes here.

File details

Details for the file prune_source-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

  • Download URL: prune_source-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl
  • Upload date:
  • Size: 226.5 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.34+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"EndeavourOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for prune_source-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 eb55504548e87a94b7df2cf284a65ae9c7b78325deab590486622f9523ee98cf
MD5 2e5ba98f736fa897bc2ec462e362395b
BLAKE2b-256 360f6aa4ebe1b7424462bd8a1d7101ca853999c6c07eb064bd3849a50ef77069

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.4

6 files

0.1.3

6 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page