drresult Module

The drresult module provides a radical approach to Rust’s std::result in Python, enabling error handling without exceptions.

class drresult.Ok(value)[source]

Bases: BaseResult, Generic

Represents a successful result.

Parameters:

value (T)

property value: T
is_ok()[source]

Check if the result is Ok.

Return type:

bool

Returns:

bool: True.

is_err()[source]

Check if the result is an Err.

Return type:

bool

Returns:

bool: False.

expect(msg)[source]

Return the value, ignoring the message.

Return type:

TypeVar(T)

Parameters:

msg (str)

Args:

msg (str): Message to display if the result is Err.

Returns:

T: The successful value.

unwrap()[source]

Return the successful value.

Return type:

TypeVar(T)

Returns:

T: The value held by Ok.

expect_err(msg)[source]

Raise an AssertionError because the result is Ok.

Return type:

NoReturn

Parameters:

msg (str)

Args:

msg (str): Error message.

Raises:

AssertionError: Indicating unexpected call.

unwrap_err()[source]

Raise an AssertionError because the result is Ok.

Return type:

NoReturn

Raises:

AssertionError: Indicating unexpected call.

unwrap_or(alternative)[source]

Return the successful value.

Return type:

TypeVar(T)

Parameters:

alternative (U)

Args:

alternative (U): An alternative value (ignored).

Returns:

T: The value held by Ok.

unwrap_or_raise()[source]

Return the successful value.

Return type:

TypeVar(T)

Returns:

T: The value held by Ok.

unwrap_or_return()[source]

Return the successful value.

Return type:

TypeVar(T)

Returns:

T: The value held by Ok.

class drresult.Err(error)[source]

Bases: BaseResult, Generic

Represents an error result.

Parameters:

error (E)

trace()[source]

Get the formatted traceback of the error.

Return type:

str

Returns:

str: The traceback string.

property error: E
is_ok()[source]

Check if the result is Ok.

Return type:

bool

Returns:

bool: False.

is_err()[source]

Check if the result is an Err.

Return type:

bool

Returns:

bool: True.

expect(msg)[source]

Raise an AssertionError with the given message.

Return type:

NoReturn

Parameters:

msg (str)

Args:

msg (str): Error message.

Raises:

AssertionError: Indicating unexpected call.

unwrap()[source]

Raise an AssertionError because the result is Err.

Return type:

NoReturn

Raises:

AssertionError: Indicating unexpected call.

expect_err(msg)[source]

Return the error exception.

Return type:

TypeVar(E, bound= BaseException)

Parameters:

msg (str)

Args:

msg (str): Message to display if the result is Ok.

Returns:

E: The exception held by Err.

unwrap_err()[source]

Return the error exception.

Return type:

TypeVar(E, bound= BaseException)

Returns:

E: The exception held by Err.

unwrap_or(alternative)[source]

Return the alternative value.

Return type:

TypeVar(U)

Parameters:

alternative (U)

Args:

alternative (U): An alternative value to return.

Returns:

U: The alternative value.

unwrap_or_raise()[source]

Raise the stored exception.

Return type:

NoReturn

Raises:

BaseException: The exception held by Err.

unwrap_or_return()[source]

Raise the stored exception.

Return type:

NoReturn

Raises:

BaseException: The exception held by Err.

exception drresult.Panic(unhandled_exception)[source]

Bases: Exception

Exception raised for unexpected errors leading to program termination.

Attributes:

unhandled_exception (BaseException): The original unhandled exception.

Parameters:

unhandled_exception (BaseException)

trace()[source]

Get the formatted traceback and exception message.

Return type:

str

Returns:

str: The traceback and exception message.

drresult.noexcept(func)[source]

Decorator to mark a function as not expecting any exceptions.

Return type:

Callable[..., TypeVar(T)]

Parameters:

func (Callable[[...], T])

Args:

func (Callable[…, T]): The function to decorate.

Returns:

Callable[…, T]: The wrapped function.

Raises:

Panic: If an unexpected exception occurs.

drresult.returns_result(*decorator_args, **decorator_kwargs)[source]

Decorator to wrap expected exceptions in an Err, and unexpected ones in a Panic.

Can be used with or without arguments.

Return type:

Union[Callable[..., GenericAlias[TypeVar(T)]], Callable[[Callable[..., GenericAlias[TypeVar(T)]]], Callable[..., GenericAlias[TypeVar(T)]]]]

Parameters:
  • decorator_args (Any)

  • decorator_kwargs (Any)

Usage:

@returns_result def func(…): …

@returns_result(expects=[ValueError]) def func(…): …

Args:

*decorator_args (Any): Positional arguments. **decorator_kwargs (Any): Keyword arguments.

Returns:

Callable: The decorator or wrapped function.

class drresult.gather_result(expects=[<class 'Exception'>], not_expects=[<class 'AssertionError'>, <class 'AttributeError'>, <class 'ImportError'>, <class 'NameError'>, <class 'SyntaxError'>, <class 'TypeError'>, <class 'MemoryError'>, <class 'SystemError'>])[source]

Bases: Generic

Context manager to capture exceptions and convert them into a Result.

Usage:
with gather_result() as result_container:

# Code that might raise exceptions result_container.set(Ok(value))

result = result_container.get()

Parameters:
  • expects (List[Type[BaseException]])

  • not_expects (List[Type[BaseException]])

drresult.constructs_as_result(*decorator_args, **decorator_kwargs)[source]

Decorator to wrap class instantiation in a Result type.

Can be used with or without arguments.

Return type:

Any

Parameters:
  • decorator_args (Any)

  • decorator_kwargs (Any)

Usage:

@constructs_as_result class MyClass: …

@constructs_as_result(expects=[ValueError]) class MyClass: …

Args:

*decorator_args (Any): Positional arguments. **decorator_kwargs (Any): Keyword arguments.

Returns:

Any: The decorated class.

drresult.log_panic(logger)[source]

Context manager to log Panic exceptions.

Args:

logger (logging.Logger): The logger to use for logging.

Usage:
with log_panic(logger):

# Code that might raise a Panic pass

Parameters:

logger (Logger)