Mojo struct
PythonModuleBuilder
struct PythonModuleBuilder
A builder for creating Python modules with Mojo function and type bindings.
This builder provides a high-level API for declaring Python bindings for Mojo functions and types within a Python module. It manages the registration of functions, types, and their associated metadata, then finalizes everything into a complete Python module object.
The builder follows a declarative pattern where you:
- Create a builder instance with a module name
- Add function bindings using
def_function()
,def_py_function()
,def_py_c_function()
- Add type bindings using
add_type[T]()
and configure them - Call
finalize()
to finish building the Python module.
Example:
from python.bindings import PythonModuleBuilder
var builder = PythonModuleBuilder("my_module")
builder.def_function[my_func]("my_func", "Documentation for my_func")
_ = builder.add_type[MyType]("MyType").def_method[my_method]("my_method")
var module = builder.finalize()
from python.bindings import PythonModuleBuilder
var builder = PythonModuleBuilder("my_module")
builder.def_function[my_func]("my_func", "Documentation for my_func")
_ = builder.add_type[MyType]("MyType").def_method[my_method]("my_method")
var module = builder.finalize()
Note:
After calling finalize()
, the builder's internal state is cleared and
it should not be reused for creating additional modules.
TODO: This should be enforced programmatically in the future.
Fields
- module (
TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")]
): The Python module being built. - functions (
List[PyMethodDef]
): List of function definitions that will be exposed in the module. - type_builders (
List[PythonTypeBuilder]
): List of type builders for types that will be exposed in the module.
Implemented traits
AnyType
,
UnknownDestructibility
Methods
__init__
__init__(out self, name: StringSlice[StaticConstantOrigin])
Construct a Python module builder with the given module name.
Args:
- name (
StringSlice[StaticConstantOrigin]
): The name of the module.
Raises:
If the module creation fails.
__init__(out self, module: TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")])
Construct a Python module builder with the given module.
Args:
- module (
TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")]
): The module to build.
add_type
add_type[T: Movable & Defaultable & Representable & TypeIdentifiable](mut self, type_name: StringSlice[StaticConstantOrigin]) -> ref [*[0,0].type_builders] PythonTypeBuilder
Add a type to the module and return a builder for it.
Parameters:
- T (
Movable & Defaultable & Representable & TypeIdentifiable
): The mojo type to bind in the module.
Args:
- type_name (
StringSlice[StaticConstantOrigin]
): The name of the type to expose in the module.
Returns:
A reference to a type builder registered in the module builder.
def_py_c_function
def_py_c_function(mut self, func: fn(PyObjectPtr, PyObjectPtr) -> PyObjectPtr, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PyCFunction signature in the module.
Args:
- func (
fn(PyObjectPtr, PyObjectPtr) -> PyObjectPtr
): The function to declare a binding for. - func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_py_function
def_py_function[func: fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PyFunction signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_py_function[func: fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) raises -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PyFunctionRaising signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) raises -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function
def_function[func: fn() raises -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn() raises -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject) raises -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject) raises -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject) raises -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject) raises -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn() -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn() -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject) -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject) -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject) -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject) -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject, mut PythonObject) -> PythonObject](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject, mut PythonObject) -> PythonObject
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn() raises -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn() raises -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject) raises -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject) raises -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject) raises -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject) raises -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn() -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn() -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject) -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject) -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject) -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject) -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
def_function[func: fn(mut PythonObject, mut PythonObject, mut PythonObject) -> None](mut self, func_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice())
Declare a binding for a function with PythonObject signature in the module.
Parameters:
- func (
fn(mut PythonObject, mut PythonObject, mut PythonObject) -> None
): The function to declare a binding for.
Args:
- func_name (
StringSlice[StaticConstantOrigin]
): The name with which the function will be exposed in the module. - docstring (
StringSlice[StaticConstantOrigin]
): The docstring for the function in the module.
finalize
finalize(mut self) -> TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")]
Finalize the module builder, creating the module object.
All types and functions added to the builder will be built and exposed in the module. After calling this method, the builder's internal state is cleared and it should not be reused for creating additional modules.
Returns:
The finalized Python module containing all registered functions and types.
Raises:
If the module creation fails or if we fail to add any of the declared functions or types to the module.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!