Skip to main content
Log in

Mojo struct

PythonTypeBuilder

struct PythonTypeBuilder

A builder for a Python 'type' binding.

This is typically used to build a type description of a PyMojoObject[T].

This builder is used to declare method bindings for a Python type, and then create the type binding.

Finalizing builder created with PythonTypeObject.bind[T]() will globally register the resulting Python 'type' object as the single canonical type object for the Mojo type T. Subsequent attempts to register a Python type for T will raise an exception.

Registering a Python type object for T is necessary to be able to construct a PythonObject from an instance of T, or to downcast an existing PythonObject to a pointer to the inner T value.

Fields

  • type_name (StringSlice[StaticConstantOrigin]): The name the type will be exposed as in the Python module.
  • basicsize (Int): The required allocation size to hold an instance of this type as a Python object.
  • methods (List[PyMethodDef]): List of method definitions that will be exposed on the Python type.

Implemented traits

AnyType, Copyable, Movable, UnknownDestructibility

Methods

__init__

__init__(out self, type_name: StringSlice[StaticConstantOrigin], *, basicsize: Int)

Construct a new builder for a Python type binding.

Args:

  • type_name (StringSlice[StaticConstantOrigin]): The name the type will be exposed as in the Python module.
  • basicsize (Int): The required allocation size to hold an instance of this type as a Python object.

bind

static bind[T: Movable & Defaultable & Representable & TypeIdentifiable](type_name: StringSlice[StaticConstantOrigin]) -> Self

Construct a new builder for a Python type that binds a Mojo type.

Parameters:

  • T (Movable & Defaultable & Representable & TypeIdentifiable): The mojo type to bind.

Args:

  • type_name (StringSlice[StaticConstantOrigin]): The name the type will be exposed as in the Python module.

Returns:

A new type builder instance.

finalize

finalize(mut self) -> TypedPythonObject[__init__[__mlir_type.!kgen.string]("Type")]

Finalize the builder and create a Python type object.

This method completes the construction of a Python type object from the builder's configuration.

The method ensures that each Mojo type has exactly one corresponding Python type object by registering the created type in a global registry. This prevents accidental creation of multiple type objects for the same Mojo type, which would break Python's type system assumptions.

Note: After calling this method, the builder's internal state may be modified (methods list is consumed), so the builder should not be reused for creating additional type objects.

TODO: This should be enforced programmatically in the future.

Returns:

A TypedPythonObject["Type"] representing the newly created Python type object that can be used to create instances or register with Python modules.

Raises:

If the Python type object creation fails, typically due to invalid type specifications or Python C API errors.

finalize(mut self, module: TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")])

Finalize the builder and add the created type to a Python module.

This method completes the type building process by calling the parameterless finalize() method to create the Python type object, then automatically adds the resulting type to the specified Python module using the builder's configured type name. After successful completion, the builder's method list is cleared to prevent accidental reuse.

This is a convenience method that combines type finalization and module registration in a single operation, which is the most common use case when creating Python-accessible Mojo types.

Note: After calling this method, the builder's internal state is modified (methods list is cleared), so the builder should not be reused for creating additional type objects. If you need the type object for further operations, use the parameterless finalize() method instead and manually add it to the module.

Args:

  • module (TypedPythonObject[__init__[__mlir_type.!kgen.string]("Module")]): The Python module to which the finalized type will be added. The type will be accessible from Python code that imports this module using the name specified during builder construction.

Raises:

If the type object creation fails (see finalize() for details) or if adding the type to the module fails, typically due to name conflicts or module state issues.

def_py_c_method

def_py_c_method(mut self, method: fn(PyObjectPtr, PyObjectPtr) -> PyObjectPtr, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PyObjectPtr signature for the type.

Args:

  • method (fn(PyObjectPtr, PyObjectPtr) -> PyObjectPtr): The method to declare a binding for.
  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_py_method

def_py_method[method: fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PyObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_py_method[method: fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) raises -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PyObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut TypedPythonObject[__init__[__mlir_type.!kgen.string]("Tuple")]) raises -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method

def_method[method: fn(mut PythonObject) raises -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject) raises -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject) raises -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject) raises -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject) -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject) -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject) -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject) -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject, mut PythonObject) -> PythonObject](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject, mut PythonObject) -> PythonObject): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject) raises -> None](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject) raises -> None): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject) raises -> None](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject) raises -> None): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> None](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject, mut PythonObject) raises -> None): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject) -> None](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject) -> None): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject) -> None](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject) -> None): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.

def_method[method: fn(mut PythonObject, mut PythonObject, mut PythonObject) -> None](mut self, method_name: StringSlice[StaticConstantOrigin], docstring: StringSlice[StaticConstantOrigin] = StringSlice()) -> ref [*[0,0]] Self

Declare a binding for a method with PythonObject signature for the type.

Parameters:

  • method (fn(mut PythonObject, mut PythonObject, mut PythonObject) -> None): The method to declare a binding for.

Args:

  • method_name (StringSlice[StaticConstantOrigin]): The name with which the method will be exposed on the type.
  • docstring (StringSlice[StaticConstantOrigin]): The docstring for the method of the type.

Returns:

The builder with the method binding declared.