babylon.engine.result

Result type for total functions with explicit error channels.

Spec 040, Discipline 2: Systems return Result[WorldState, TransitionError]. Exceptions are reserved for programmer errors (shape mismatches, missing nodes). Expected failures go through the Result channel.

Usage:

from babylon.engine.result import Ok, Err, Result

def step(state: WorldState) -> Result[WorldState, TransitionError]:
    if wealth < 0:
        return Err(NegativeCapitalStock(node_id="C001", field="wealth", value=wealth))
    return Ok(state.model_copy(update={"tick": state.tick + 1}))

Module Attributes

Result

Type alias for the Result union.

Classes

Err(error)

Error variant of Result.

Ok(value)

Success variant of Result.

class babylon.engine.result.Ok(value)[source]

Bases: Generic

Success variant of Result.

Holds the successful value from a system step.

Parameters:

value (TypeVar(T)) – The successful result value.

value: TypeVar(T)
is_ok()[source]

Return True — this is a success variant.

Return type:

bool

is_err()[source]

Return False — this is not an error variant.

Return type:

bool

unwrap()[source]

Return the contained value.

Return type:

TypeVar(T)

Returns:

The success value.

map(fn)[source]

Apply fn to the contained value.

Parameters:

fn (Callable[[TypeVar(T)], TypeVar(U)]) – Function to apply to the success value.

Return type:

Ok[TypeVar(U)]

Returns:

New Ok with transformed value.

__init__(value)
Parameters:

value (T)

Return type:

None

class babylon.engine.result.Err(error)[source]

Bases: Generic

Error variant of Result.

Holds a modeled failure from a system step.

Parameters:

error (TypeVar(E)) – The error describing what went wrong.

error: TypeVar(E)
is_ok()[source]

Return False — this is not a success variant.

Return type:

bool

is_err()[source]

Return True — this is an error variant.

Return type:

bool

unwrap()[source]

Raise ValueError — cannot unwrap an Err.

Raises:

ValueError – Always, with the error message.

Return type:

None

map(_fn)[source]

Pass through — errors are not transformed by map.

Parameters:

_fn (Callable[..., object]) – Ignored function.

Return type:

Err[TypeVar(E)]

Returns:

Self unchanged.

__init__(error)
Parameters:

error (E)

Return type:

None

babylon.engine.result.Result

Type alias for the Result union.

A Result[T, E] is either Ok[T] (success) or Err[E] (modeled failure).

alias of Ok[T] | Err[E]