data_tools.schema.Result#

class data_tools.schema.Result(result: T, result_type: ResultType)#

Bases: Generic

Algebraic datatype that can either represent a successful result of some operation, or a failure.

A Result can be unwrapped to retrieve the result or raise the exception raised wrapped in a UnwrappedError.

__init__(result: T, result_type: ResultType)#

Do not call this directly. Call Result.Ok() or Result.Err().

Parameters:
  • result (T) – Data or exception to be wrapped.

  • result_type (ResultType) – Whether the result is data or an exception.

Raises:

TypeError – If trying to wrap data as an Exception or an Exception as data

Methods

Err(error)

Wrap an error/failure/exception in a Result.

Ok(result)

Wrap a successful result in a Result.

__init__(result, result_type)

Do not call this directly.

unwrap()

Unwrap this Result to reveal a successful result or an error.

static Err(error: Exception)#

Wrap an error/failure/exception in a Result.

Parameters:

error – The error to be wrapped

Returns:

A Result instance wrapping error.

Raises:

TypeError – If error is not an Exception.

static Ok(result: T)#

Wrap a successful result in a Result.

Parameters:

result – The successful result to be wrapped

Returns:

A Result instance wrapping result.

Raises:

TypeError – If result is an Exception.

class ResultType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: StrEnum

Err = 'Err'#
Ok = 'Ok'#
unwrap() T | UnwrappedError#

Unwrap this Result to reveal a successful result or an error.

Raises:

UnwrappedError – If an error is unwrapped

Returns:

The result, if it was successful