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,GenericRepresents a successful result.
- Parameters:
value (T)
- property value: T
- 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.
- class drresult.Err(error)[source]
Bases:
BaseResult,GenericRepresents 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
- 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.
- exception drresult.Panic(unhandled_exception)[source]
Bases:
ExceptionException raised for unexpected errors leading to program termination.
- Attributes:
unhandled_exception (BaseException): The original unhandled exception.
- Parameters:
unhandled_exception (BaseException)
- 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)
- 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:
GenericContext 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]])