result

Defines the result type and its variants: success and failure.

The result type is a union type of Success and Failure respectively containing a successful value or an error code.

It is mostly meant to be used as a return type for functions that can fail, but where we want to be sure to handle all cases in the calling code and not raise unhandled exceptions.

With a type checker, we can ensure that all possible success and failure cases are dealt with.

Example

from typing import assert_never

from caqtus.utils.result import Success, Failure, is_success, is_failure_type

def read_file(path) -> Success[str] | Failure[FileNotFoundError]:
    try:
        with open(path) as file:
            return Success(file.read())
    except FileNotFoundError as error:
        return Failure(error)

result = read_file("file.txt")
if is_failure_type(result, FileNotFoundError):
    print("File not found")
elif is_success(result):
    print(result.content())
else:
    assert_never(result)

Functions

is_failure(result)

Check if a result is a failure.

is_failure_type(result, error_type)

Check if a result is a failure and contains a specific error type.

is_success(result)

Check if a result is a success.

unwrap(-> ~caqtus.utils.result._result.T)

Unwrap a result when the failure case is an exception.

Classes

Failure(error)

A failed result containing an error code of type E.

Success(value)

A successful result containing a value of type T.