Class Result

class ooodev.utils.result.Result(data, error)[source]

A generic Result type that can represent either a successful operation with data or a failure with an error.

Type Parameters:

T: The type of the success value E: The type of the error value, must be BaseException or None

Examples

>>> result = Result.success(10)
>>> if Result.is_success(result):
>>>     print(result.data)  # Outputs: 10
>>> else:
>>>     print(result.error)
>>> result = Result.failure(ValueError("Invalid input"))
>>> data, error = result.unpack()
>>> print(error)  # Outputs: Invalid input

New in version 0.53.0.

__bool__()[source]

Check if the Result represents success.

Return type:

bool

Returns:

True if the Result represents success, False otherwise

__eq__(value)[source]

Check if two Result instances are equal.

Parameters:

value (object) – The other Result instance to compare with

Return type:

bool

Returns:

True if the two instances are equal, False otherwise

__init__(data, error)[source]

Initialize a Result instance.

Parameters:
  • data (TypeVar(T)) – The success value

  • error (TypeVar(E, bound= Optional[BaseException])) – The error value

Return type:

None

__iter__()[source]

Make Result instance iterable.

Return type:

Iterator[Union[TypeVar(T), TypeVar(E, bound= Optional[BaseException])]]

Returns:

Iterator yielding data and error values

__repr__()[source]

Get the string representation of the Result instance.

Return type:

str

Returns:

A string representation of the Result instance

static failure(error)[source]

Create a failure Result with the given error.

Parameters:

error (TypeVar(E_Failure, bound= Optional[BaseException])) – The error value

Return type:

Result[None, E_Failure]

Returns:

A Result instance representing failure

static is_failure(obj)[source]

Type guard to check if a Result instance represents failure.

Parameters:

obj (Union[Result[T_Success, None], Result[None, E_Failure]]) – The Result instance to check

Return type:

TypeIs[Result[None, E_Failure]]

Returns:

True if the Result represents failure, False otherwise

static is_success(obj)[source]

Type guard to check if a Result instance represents success.

Parameters:

obj (Union[Result[T_Success, None], Result[None, E_Failure]]) – The Result instance to check

Return type:

TypeIs[Result[T_Success, None]]

Returns:

True if the Result represents success, False otherwise

static success(data)[source]

Create a successful Result with the given data.

Parameters:

data (TypeVar(T_Success)) – The success value

Return type:

Result[T_Success, None]

Returns:

A Result instance representing success

unpack()[source]

Unpack the Result into a tuple of (data, error).

Return type:

Tuple[TypeVar(T), TypeVar(E, bound= Optional[BaseException])]

Returns:

A tuple containing the data and error values