Registry / serialization / ast-tools

ast-tools

JSON →
library0.1.8pypypiunverified

ast-tools is a Python library providing a toolbox for working with and transforming Python Abstract Syntax Trees (ASTs). It offers utilities like `MutableAST` for in-place modifications and a `Visitor` for traversing ASTs, simplifying complex AST manipulations. The current version is 0.1.8, and its release cadence is infrequent, with the last release in June 2022.

pip install ast-tools
INSTALL
IMPORT
SIG · AST-TOOLS
A
ast-tools
serializationpythonv0.1.8
Install
2.4s avg
Import
Disk
32MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.8 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 32.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.4s · import 0.000s · 34MB
32MB installed
● package 32MB
Code
Verified usage

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

MutableAST
from ast_tools.mutable_ast import MutableAST
from ast_tools import MutableAST
Classes are in specific submodules, not directly under the top-level package.
function_to_ast
from ast_tools.common import function_to_ast
from ast_tools import function_to_ast
Helper functions like function_to_ast and eval_ast reside in the 'common' submodule.
Visitor
from ast_tools.stack_vm import Visitor
from ast_tools.visitor import Visitor
The AST Visitor class is part of the 'stack_vm' submodule, which might not be immediately intuitive.

This quickstart demonstrates converting a Python function to a mutable AST, modifying a binary operation within it, and then executing the transformed AST as a new function. It highlights the use of `MutableAST` for in-place modifications and `function_to_ast` / `eval_ast` for conversion.

import ast from ast_tools.mutable_ast import MutableAST from ast_tools.common import function_to_ast, eval_ast def add_func(a, b): c = a + b return c # Convert a Python function to a mutable AST representation m = MutableAST(add_func) # Example transformation: change '+' to '-' in the binary operation for node in m.body: if isinstance(node, ast.Assign): if isinstance(node.value, ast.BinOp) and isinstance(node.value.op, ast.Add): node.value.op = ast.Sub() break # Assuming only one assignment in this simple example # Convert the modified AST back into a runnable function sub_func = eval_ast(m.ast) # Test the transformed function result = sub_func(10, 5) print(f"Original function output (if run directly): {add_func(10, 5)}") print(f"Transformed function output: {result}") # Expected: 5
Debug
Known issues
gotchaast-tools builds upon Python's standard `ast` module. A solid understanding of the `ast` module, AST node types, and AST traversal/manipulation patterns is highly recommended for effective use of ast-tools.
fix
Familiarize yourself with the `ast` module documentation and common AST manipulation techniques.
affects: All versions
gotchaWhen manually constructing or heavily modifying `ast` nodes (e.g., creating new `ast.Constant` or `ast.Name` nodes), you may need to call `ast.fix_missing_locations(node)` to ensure that line numbers and column offsets are correctly set. Without this, compiling the AST can lead to errors.
fix
Always run `ast.fix_missing_locations(your_modified_ast_root)` on any AST that you've manually constructed or altered significantly, before passing it to `compile()` or `eval_ast`.
affects: All versions
deprecatedThe project shows infrequent updates. While functional, it might not track the absolute latest Python AST changes or receive new features frequently. For critical projects, evaluate its long-term maintenance status.
fix
Monitor the GitHub repository for activity and consider contributing or forking if specific modern Python features require AST support not present. For critical applications, ensure thorough testing.
affects: 0.1.x and potentially future versions
Upgrade
Version history
0.1.8latest on PyPI · released Oct 1, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
18
OpenAI (training)
1
Resources
ast-tools — pip install ast-tools · libregistry