result package

A fully type-annotated Rust-like Result type for Python.

class Err(_error)[source]

Bases: result._core._ResultMixin[result._core.T, result._core.E]

Err result type.

A value that signifies failure and which stores arbitrary data for the error.

Parameters

_error (~E) –

err()[source]

Returns None if successful or an Exception type otherwise.

Return type

~E

unwrap()[source]

Returns real return type or raises an exception if unsuccessful.

Return type

NoReturn

unwrap_or(default)[source]

Returns real return type if successful or default otherwise.

Parameters

default (~T) –

Return type

~T

unwrap_or_else(op)[source]

Returns real return type if successful or op(e) otherwise.

Parameters

op (Callable[[~E], ~T]) –

Return type

~T

class LazyResult(func, *args, **kwargs)[source]

Bases: result._core._ResultMixin[result._core.T, result._core.E]

See help(return_lazy_result).

Parameters
  • func (Callable[…, Union[Ok[~T, ~E], Err[~T, ~E]]]) –

  • args (Any) –

  • kwargs (Any) –

err()[source]

Returns None if successful or an Exception type otherwise.

Return type

Optional[~E]

result()[source]

Retrieve the Result object corresponding with this LazyResult.

Calls the function corresponding with this LazyResult (this function will only be called once, even if this method is called multiple times) and returns the same Result returned by that function.

Return type

Union[Ok[~T, ~E], Err[~T, ~E]]

unwrap()[source]

Returns real return type or raises an exception if unsuccessful.

Return type

~T

unwrap_or(default)[source]

Returns real return type if successful or default otherwise.

Parameters

default (~T) –

Return type

~T

unwrap_or_else(op)[source]

Returns real return type if successful or op(e) otherwise.

Parameters

op (Callable[[~E], ~T]) –

Return type

~T

class Ok(_value)[source]

Bases: result._core._ResultMixin[result._core.T, result._core.E]

Ok result type.

A value that indicates success and which stores arbitrary data for the return value.

Parameters

_value (~T) –

static err()[source]

Returns None if successful or an Exception type otherwise.

Return type

None

ok()[source]
Return type

~T

unwrap()[source]

Returns real return type or raises an exception if unsuccessful.

Return type

~T

unwrap_or(default)[source]

Returns real return type if successful or default otherwise.

Parameters

default (~T) –

Return type

~T

unwrap_or_else(op)[source]

Returns real return type if successful or op(e) otherwise.

Parameters

op (Callable[[~E], ~T]) –

Return type

~T

return_lazy_result(func)[source]

Converts the return type of a function from result to a “lazy” result.

In order to fetch the real return type from lazy_result, you must call lazy_result.result() or any other valid Result method [e.g. lazy_result.unwrap()].

This decorator is useful when dealing with functions that return Result[None, E] (i.e. functions that are used soley for their side-effects), since it makes it harder to ignore potential errors.

Parameters

func (Callable[…, Union[Ok[~T, ~E], Err[~T, ~E]]]) –

Return type

Callable[…, LazyResult[~T, ~E]]