provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code.
Project description
astlab
astlab is a Python library that provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code. With astlab, you can easily construct Python modules, classes, functions, type aliases, and generics using a fluent API — then render them into valid, executable Python code.
Features
- Easy AST construction: Build Python code using a fluent, structured API.
- Code generation: Generate fully valid, formatted Python source without templates.
- Supports nested scopes & auto imports: Create classes, methods, and nested modules with automatic import resolution.
- Type system support: Define and use type variables, generic classes, and type aliases compatible with Python 3.9–3.14 syntax.
- Highly customizable: Extend the builder model for any Python AST use case.
Installation
pip install astlab
Usage
Simple Example
import ast
import astlab
with astlab.module("foo") as foo:
with foo.class_def("Bar").dataclass() as bar:
bar.field_def("spam", int)
print(foo.render())
print(ast.dump(foo.build(), indent=4))
Output
import builtins
import dataclasses
@dataclasses.dataclass()
class Bar:
spam: builtins.int
Function Definition & Call Example
import astlab
with astlab.module("foo") as foo:
with foo.class_def("Bar") as bar:
with bar.method_def("do_stuff").arg("spam", int).returns(str) as stuff:
stuff.assign_stmt("result", stuff.call(str).arg(stuff.attr("spam")))
stuff.return_stmt(stuff.attr("result"))
print(foo.render())
Output
import builtins
class Bar:
def do_stuff(self, spam: builtins.int) -> builtins.str:
result = builtins.str(spam)
return result
Type Reference Example
import astlab
with astlab.package("main") as main:
with main.module("foo") as foo:
with foo.class_def("Bar") as bar:
pass
with main.module("spam") as spam:
with spam.class_def("Eggs").inherits(bar) as eggs:
with eggs.method_def("do_stuff").returns(bar.ref().optional()) as stuff:
pass
print(spam.render())
Output
import main.foo
import typing
class Eggs(main.foo.Bar):
def do_stuff(self) -> typing.Optional[main.foo.Bar]:
pass
Generics and Type Variables
astlab supports defining type variables and generic classes.
Both the legacy (typing.TypeVar) and modern (class Node[T: int]) syntaxes are supported depending on Python version.
Example
import astlab
with astlab.module("generic") as mod:
with mod.class_def("Node") as node, node.type_var("T").lower(int) as T:
node.field_def("value", T)
node.field_def("parent", node.ref().type_params(type_var).optional(), mod.none())
print(mod.render())
Output (python < 3.12)
import builtins
import typing
T = typing.TypeVar('T', bound=builtins.int)
class Node(typing.Generic[T]):
value: T
parent: typing.Optional['Node[T]'] = None
Output (python 3.12, 3.13)
import builtins
import typing
class Node[T: builtins.int]:
value: T
parent: typing.Optional['Node[T]'] = None
Output (python ≥ 3.14)
import builtins
import typing
class Node[T: builtins.int]:
value: T
parent: typing.Optional[Node[T]] = None
Type Aliases
astlab allows declarative creation of type aliases, including recursive and generic aliases.
It automatically emits valid syntax for both typing.TypeAlias (pre-3.12) and type X = Y (3.12+).
Example
import astlab
from astlab.types import predef
with astlab.module("alias") as mod:
mod.type_alias("MyInt").assign(int)
with mod.type_alias("Json") as json_alias:
json_alias.assign(
json_alias.union_type(
None,
bool,
int,
float,
str,
mod.list_type(json_alias),
mod.dict_type(str, json_alias),
)
)
with (
mod.type_alias("Nested") as nested_alias,
nested_alias.type_var("T") as T,
):
nested_alias.assign(
nested_alias.union_type(
T
nested_alias.sequence_type(nested_alias.type_params(T)),
)
)
Output (python < 3.12)
import builtins
import typing
MyInt: typing.TypeAlias = builtins.int
Json: typing.TypeAlias = typing.Union[
None,
builtins.bool,
builtins.int,
builtins.float,
builtins.str,
builtins.list['Json'],
builtins.dict[builtins.str, 'Json'],
]
T = typing.TypeVar("T")
Nested: typing.TypeAlias = typing.Union[T, typing.Sequence['Nested[T]']]
Output (python 3.12, 3.13)
import builtins
import typing
type MyInt = builtins.int
type Json = typing.Union[
None,
builtins.bool,
builtins.int,
builtins.float,
builtins.str,
builtins.list['Json'],
builtins.dict[builtins.str, 'Json'],
]
type Nested[T] = typing.Union[T, typing.Sequence['Nested[T]']]
Output (python ≥ 3.14)
import builtins
import typing
type MyInt = builtins.int
type Json = typing.Union[
None,
builtins.bool,
builtins.int,
builtins.float,
builtins.str,
builtins.list[Json],
builtins.dict[builtins.str, Json],
]
type Nested[T] = typing.Union[T, typing.Sequence[Nested[T]]]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file astlab-0.5.0.tar.gz.
File metadata
- Download URL: astlab-0.5.0.tar.gz
- Upload date:
- Size: 25.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04d23d5a392fc622caf7ecf15fb5530ed54a78f005de15a5cba85680a660d06c
|
|
| MD5 |
c1e812aae2a78106a3c81c2b0aa5e536
|
|
| BLAKE2b-256 |
08176d870dece105d81679a2404fa90b58c1bc66515ccb18933c11581d798ab9
|
Provenance
The following attestation bundles were made for astlab-0.5.0.tar.gz:
Publisher:
publish.yaml on zerlok/astlab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astlab-0.5.0.tar.gz -
Subject digest:
04d23d5a392fc622caf7ecf15fb5530ed54a78f005de15a5cba85680a660d06c - Sigstore transparency entry: 620778692
- Sigstore integration time:
-
Permalink:
zerlok/astlab@c2cb2276a0eae72ab4dc996c6d85bff20ef5c28c -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/zerlok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@c2cb2276a0eae72ab4dc996c6d85bff20ef5c28c -
Trigger Event:
release
-
Statement type:
File details
Details for the file astlab-0.5.0-py3-none-any.whl.
File metadata
- Download URL: astlab-0.5.0-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c7d763639fb62bf268ddeedbea543de8bd67b5ecf40b14f216894babc88703a
|
|
| MD5 |
faccba35440e9068d37c1ee40b37a19c
|
|
| BLAKE2b-256 |
51357899934031f8a9b76094230202262853c315ce8db24d94f400ce218292b8
|
Provenance
The following attestation bundles were made for astlab-0.5.0-py3-none-any.whl:
Publisher:
publish.yaml on zerlok/astlab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astlab-0.5.0-py3-none-any.whl -
Subject digest:
9c7d763639fb62bf268ddeedbea543de8bd67b5ecf40b14f216894babc88703a - Sigstore transparency entry: 620778694
- Sigstore integration time:
-
Permalink:
zerlok/astlab@c2cb2276a0eae72ab4dc996c6d85bff20ef5c28c -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/zerlok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@c2cb2276a0eae72ab4dc996c6d85bff20ef5c28c -
Trigger Event:
release
-
Statement type: