GitHub

API reference for result.

Ok[A] | Err[E]: success or a typed error, as a plain value.

Test which one you have with match (case Ok(v) / case Err(e)), isinstance, or the is_ok / is_err guards. The methods exist on both classes, so they work on a Result without narrowing first. Effect.result() produces one.

AttributeTypeDescription
A_co= TypeVar('A_co', covariant=True)
E_co= TypeVar('E_co', covariant=True)

Classes

Functions

is_ok

is_ok(result) -> TypeIs[Ok[A]]
ParameterTypeDescription
resultResult[A, E]

Returns TypeIs[Ok[A]]

def is_ok[A, E](result: Result[A, E]) -> TypeIs[Ok[A]]:
    return isinstance(result, Ok)

is_err

is_err(result) -> TypeIs[Err[E]]
ParameterTypeDescription
resultResult[A, E]

Returns TypeIs[Err[E]]

def is_err[A, E](result: Result[A, E]) -> TypeIs[Err[E]]:
    return isinstance(result, Err)

try_

try_(thunk, catch=None) -> Result[Any, Any]

Ok of what thunk returns, or Err of the exception it raised (mapped through catch when given).

ParameterTypeDescription
thunkCallable[[], Any]
catchCallable[[Exception], Any] | None = None

Returns Result[Any, Any]

def try_(
    thunk: Callable[[], Any], catch: Callable[[Exception], Any] | None = None
) -> Result[Any, Any]:
    """``Ok`` of what ``thunk`` returns, or ``Err`` of the exception it
    raised (mapped through ``catch`` when given)."""
    try:
        return Ok(thunk())
    except Exception as exc:
        return Err(exc if catch is None else catch(exc))

from_optional

from_optional(value, on_none) -> Result[A, E]
ParameterTypeDescription
valueA | None
on_noneCallable[[], E]

Returns Result[A, E]

def from_optional[A = Never, E = Never](value: A | None, on_none: Callable[[], E]) -> Result[A, E]:
    return Err(on_none()) if value is None else Ok(value)

all

all(*results) -> Result[tuple[Any, ...], Any]

The values in order, or the first Err.

ParameterTypeDescription
resultsResult[Any, Any] = ()

Returns Result[tuple[Any, ...], Any]

def all(*results: Result[Any, Any]) -> Result[tuple[Any, ...], Any]:
    """The values in order, or the first ``Err``."""
    values: list[Any] = []
    for r in results:
        match r:
            case Ok(value):
                values.append(value)
            case Err():
                return r
    return Ok(tuple(values))