Result#

Result module.

The Result[TSource,TError] type lets you write error-tolerant code that can be composed. The Result type is typically used in monadic error-handling, which is often referred to as Railway-oriented Programming.

There is also a simplifyed alias of this type called Try that pins the Result type to Exception.

class Result(**kwargs: Any)#

The result class.

static Error(error: _TError) Result[_TSource, _TError]#

Create a new Error result.

static Ok(value: _TSource) Result[_TSource, _TError]#

Create a new Ok result.

bind(mapper: Callable[[_TSource], Result[_TResult, _TError]]) Result[_TResult, _TError]#

Bind result.

Return a result of the value after applying the mapping function, or Error if the input is Error.

default_value(value: _TSource) _TSource#

Get with default value.

Gets the value of the result if the result is Ok, otherwise returns the specified default value.

default_with(getter: Callable[[_TError], _TSource]) _TSource#

Get with default value lazily.

Gets the value of the result if the result is Ok, otherwise returns the value produced by the getter

dict() dict[str, _TSource | _TError | Literal['ok', 'error']]#

Return a json serializable representation of the result.

is_error() bool#

Returns True if the result is an Error value.

is_ok() bool#

Return True if the result is an Ok value.

map(mapper: Callable[[_TSource], _TResult]) Result[_TResult, _TError]#

Map result.

Return a result of the value after applying the mapping function, or Error if the input is Error.

map2(other: Result[_TOther, _TError], mapper: Callable[[_TSource, _TOther], _TResult]) Result[_TResult, _TError]#

Map result.

Return a result of the value after applying the mapping function, or Error if the input is Error.

map_error(mapper: Callable[[_TError], _TResult]) Result[_TSource, _TResult]#

Map error.

Return a result of the error value after applying the mapping function, or Ok if the input is Ok.

classmethod of_option(value: Option[_TSource], error: _TError) Result[_TSource, _TError]#

Convert option to a result.

classmethod of_option_with(value: Option[_TSource], error: Callable[[], _TError]) Result[_TSource, _TError]#

Convert option to a result.

swap() Result[_TError, _TSource]#

Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok.

to_option() Option[_TSource]#

Convert result to an option.

default_value(value: _TSource) Callable[[Result[_TSource, Any]], _TSource]#

Get the value or default value.

Gets the value of the result if the result is Ok, otherwise returns the specified default value.

default_with(getter: Callable[[_TError], _TSource]) Callable[[Result[_TSource, _TError]], _TSource]#

Get with default value lazily.

Gets the value of the option if the option is Some, otherwise returns the value produced by the getter

is_error(result: Result[_TSource, _TError]) TypeGuard[Result[_TSource, _TError]]#

Returns True if the result is an Error value.

is_ok(result: Result[_TSource, _TError]) TypeGuard[Result[_TSource, _TError]]#

Returns True if the result is an Ok value.