Result API Reference¶
API documentation for the Result type and related classes.
Type Alias¶
A type representing either success (Ok[T]) or failure (Err[E]).
Ok¶
Ok
¶
Bases: Generic[T]
Success variant of Result containing a value.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The success value to wrap.
TYPE:
|
map_async
async
¶
map_async(
fn: Callable[[T], Coroutine[Any, Any, U]],
) -> Ok[U]
Transform Ok value with async function.
map_err_async
async
¶
map_err_async(
fn: Callable[[Any], Coroutine[Any, Any, F]],
) -> Ok[T]
Return self (no error to transform).
and_then
¶
Chain Result-returning operation.
and_then_async
async
¶
Chain async Result-returning operation.
or_else
¶
Return self (no error to recover from).
or_else_async
async
¶
Return self (no error to recover from).
unwrap_or_else
¶
Return the Ok value, ignoring the function.
unwrap_or_raise
¶
Return the Ok value, ignoring the exception factory.
filter
¶
Return self if predicate passes, otherwise Err with given error.
zip
¶
Combine with another Result into a tuple.
zip_with
¶
Combine with another Result using a function.
Err¶
Err
¶
Bases: Generic[E]
Error variant of Result containing an error value.
| PARAMETER | DESCRIPTION |
|---|---|
error
|
The error value to wrap.
TYPE:
|
Example
map_async
async
¶
map_async(
fn: Callable[[Any], Coroutine[Any, Any, U]],
) -> Err[E]
Return self (no value to transform).
map_err_async
async
¶
map_err_async(
fn: Callable[[E], Coroutine[Any, Any, F]],
) -> Err[F]
Transform Err value with async function.
and_then_async
async
¶
Return self (no value to chain).
or_else
¶
Recover from error by calling fn.
or_else_async
async
¶
Recover from error with async function.
unwrap_or_raise
¶
Raise exception created by fn(error).
inspect_err
¶
inspect_err(fn: Callable[[E], Any]) -> Err[E]
Execute fn for side effects, return self.
filter
¶
filter(
predicate: Callable[[Any], bool], error: Any
) -> Err[E]
Return self unchanged (already an error).
zip_with
¶
Return self (first error wins).
LazyResult¶
LazyResult
¶
LazyResult(
source: Awaitable[Ok[T] | Err[E]] | Ok[T] | Err[E],
operations: tuple[ResultOperation, ...] = (),
)
Bases: Generic[T, E]
Lazy Result with deferred execution for clean async chaining.
LazyResult builds a pipeline of operations that execute only when
.collect() is called. All methods accept both sync and async
functions transparently, avoiding nested await chains.
This pattern is inspired by Polars' lazy evaluation - build the computation graph, then execute it all at once.
Example
Note
Operations are stored as frozen dataclasses and executed sequentially. Short-circuiting occurs on Err values.
from_result
classmethod
¶
from_result(result: Ok[T] | Err[E]) -> LazyResult[T, E]
Create LazyResult from an existing Result.
from_awaitable
classmethod
¶
from_awaitable(
awaitable: Awaitable[Ok[T] | Err[E]],
) -> LazyResult[T, E]
Create LazyResult from a coroutine/awaitable that returns Result.
map
¶
map(
fn: Callable[[T], U | Awaitable[U]],
) -> LazyResult[U, E]
Transform Ok value. fn can be sync or async.
map_err
¶
map_err(
fn: Callable[[E], F | Awaitable[F]],
) -> LazyResult[T, F]
Transform Err value. fn can be sync or async.
and_then
¶
and_then(
fn: Callable[
[T], Ok[U] | Err[E] | Awaitable[Ok[U] | Err[E]]
],
) -> LazyResult[U, E]
Chain Result-returning function. fn can be sync or async.
or_else
¶
or_else(
fn: Callable[
[E], Ok[T] | Err[F] | Awaitable[Ok[T] | Err[F]]
],
) -> LazyResult[T, F]
Recover from Err. fn can be sync or async.
tee
¶
tee(fn: Callable[[T], Any]) -> LazyResult[T, E]
Side effect on Ok value. fn can be sync or async.
inspect_err
¶
inspect_err(fn: Callable[[E], Any]) -> LazyResult[T, E]
Side effect on Err value. fn can be sync or async.
flatten
¶
flatten() -> LazyResult[U, E]
Flatten nested LazyResult[Result[U, E], E] to LazyResult[U, E].
Type Guards¶
is_ok¶
is_ok
¶
Type guard to check if a result is Ok.
Unlike the .is_ok() method, this standalone function enables
proper type narrowing in conditional branches. This is necessary
because Python's type system doesn't support narrowing based on
method return types.
This pattern is also used by rustedpy/result.
| PARAMETER | DESCRIPTION |
|---|---|
result
|
The Result to check. |
| RETURNS | DESCRIPTION |
|---|---|
TypeIs[Ok[T]]
|
True if result is Ok, with the type narrowed to Ok[T]. |
is_err¶
is_err
¶
Type guard to check if a result is Err.
Unlike the .is_err() method, this standalone function enables
proper type narrowing in conditional branches. This is necessary
because Python's type system doesn't support narrowing based on
method return types.
This pattern is also used by rustedpy/result.
| PARAMETER | DESCRIPTION |
|---|---|
result
|
The Result to check. |
| RETURNS | DESCRIPTION |
|---|---|
TypeIs[Err[E]]
|
True if result is Err, with the type narrowed to Err[E]. |
Utility Functions¶
sequence_results¶
sequence_results
¶
Collect an iterable of Results into a Result of list.
Fails fast on the first Err encountered, returning that error. If all Results are Ok, returns Ok containing a list of all values.
| PARAMETER | DESCRIPTION |
|---|---|
results
|
Iterable of Result values to collect. |
| RETURNS | DESCRIPTION |
|---|---|
Ok[list[T]] | Err[E]
|
Ok(list) if all are Ok, otherwise the first Err. |
traverse_results¶
traverse_results
¶
Map a function over items and collect Results.
Equivalent to sequence_results(map(fn, items)) but more efficient.
Fails fast on the first Err encountered.
| PARAMETER | DESCRIPTION |
|---|---|
items
|
Iterable of items to process.
TYPE:
|
fn
|
Function returning Result for each item. |
| RETURNS | DESCRIPTION |
|---|---|
Ok[list[T]] | Err[E]
|
Ok(list) if all succeed, otherwise the first Err. |
Exceptions¶
UnwrapError¶
UnwrapError
¶
Bases: Exception
Raised when unwrap() is called on an Err, or unwrap_err() on an Ok.
ChainedError¶
ChainedError
¶
Error with context chain for tracing error propagation.
Wraps an original error with additional context about where/why the error occurred. Multiple contexts can be chained.
| PARAMETER | DESCRIPTION |
|---|---|
error
|
The original error or a previous ChainedError.
TYPE:
|
context
|
Description of the context where the error occurred.
TYPE:
|
Example
err = ChainedError("invalid json", "parsing config") str(err) 'parsing config: invalid json' err2 = ChainedError(err, "loading user settings") str(err2) 'loading user settings: parsing config: invalid json'