A Python library providing a Result type for elegant error handling, inspired by Rust's Result type.
Project description
Safe Result
A Python library providing a Result type for elegant error handling, inspired by Rust's Result type.
Installation
pip install safe-result
Overview
Safe Result provides a Result type that represents either success (value) or failure (error). This allows for more explicit error handling without relying on exceptions, making your code more predictable and easier to reason about.
Key features:
- Type-safe result handling with generics support
- Decorator for automatically wrapping function returns in Result objects
- Support for both synchronous and asynchronous functions
- Built-in traceback capture for errors
Usage
Basic Usage
from safe_result import Result
# Success case
success = Result(value=42)
if not success.is_error():
print(success.value) # 42
# Error case
error = Result(error=ValueError("something went wrong"))
if error.is_error():
print(f"Error occurred: {error.error}")
Using the Decorator
The @Result.safe decorator automatically wraps function returns in a Result object:
from safe_result import Result
@Result.safe
def divide(a: int, b: int) -> float:
return a / b
# Success case
result = divide(10, 2)
if not result.is_error():
print(result.value) # 5.0
# Error case
result = divide(10, 0)
if result.is_error():
print(f"Error: {result.error}") # Error: division by zero
Async Functions
The decorator works with async functions too:
import asyncio
from safe_result import Result
@Result.safe
async def async_operation(value: int) -> int:
await asyncio.sleep(0.1)
if value == 0:
raise ValueError("Cannot process zero")
return value * 2
async def main():
# Success case
result = await async_operation(5)
print(result.unwrap()) # 10
# Error case
result = await async_operation(0)
print(result.unwrap_or(42)) # 42
asyncio.run(main())
Unwrapping Results
from safe_result import Result
# Get value or raise the stored exception
result = Result(value="hello")
value = result.unwrap() # "hello"
# Get value or return a default
error_result = Result(error=ValueError("oops"))
value = error_result.unwrap_or("default") # "default"
API Reference
Result[T, E]
Generic class representing either success (with value of type T) or failure (with error of type E).
Constructor
Result(value=None, error=None): Create a new Result with either a value or an error
Methods
is_error() -> bool: Check if the Result contains an errorunwrap() -> T: Return the value or raise the stored errorunwrap_or(default: T) -> T: Return the value or a default if there's an error
Class Methods
@Result.safe: Decorator that wraps a function to return a Result
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safe_result-1.0.0.tar.gz.
File metadata
- Download URL: safe_result-1.0.0.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
737620f08f1ab59dcf732541505190a26827a6bbf6011e61fc722eee9930ca89
|
|
| MD5 |
ea29b75043825d4b3bf5f072c301a84f
|
|
| BLAKE2b-256 |
60540467bb214f6fe999c4cc6919afd643a7e68903e9d5bc5a7eccafdb8561f4
|
File details
Details for the file safe_result-1.0.0-py3-none-any.whl.
File metadata
- Download URL: safe_result-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
072b24009259256b86b28c9ef6d9df3d2f952d52761b1f5bc78d7e0c29ec84e9
|
|
| MD5 |
089a46ba03877bfabaabd27327c9f166
|
|
| BLAKE2b-256 |
14f208e130a2242652cc22a349ae5f7b69e641d9b0951fca9e96d0e42329a243
|