Yet another implementation of Rust's Result type: minimal, dependency-free and fully-typed.
Project description
better-result-py
Inspired by better-result
Yet another implementation of Rust's Result type: minimal, dependency-free, and fully-typed. Wraps synchronous and asynchronous functions so exceptions become values rather than control flow.
Installation
# with uv
uv add better-result-py
# with pip
pip install better-result-py
Usage
Synchronous
Wrap any callable with Result. If the function raises, ok is Unset and err holds the exception. If it succeeds, ok holds the value and err is None.
from better_result import Result
def divide(a: int, b: int) -> float:
return a / b
result = Result(divide, 10, 2)
result.is_ok() # True
result.ok # 5.0
result.err # None
result = Result(divide, 10, 0)
result.is_ok() # False
result.err # ZeroDivisionError(...)
Asynchronous
AsyncResult works the same way for async functions.
import asyncio
from better_result import AsyncResult
async def fetch_data(url: str) -> str:
... # may raise
result = await AsyncResult(fetch_data, "https://example.com")
if result.is_ok():
print(result.ok)
else:
print(result.err)
Extracting values
unwrap()
Returns the value or raises the original exception (or UnsetError if the result is unset).
value = Result(divide, 10, 2).unwrap() # 5.0
Result(divide, 10, 0).unwrap() # raises ZeroDivisionError
unwrap_or(default)
Returns the value or falls back to a default on error.
value = Result(divide, 10, 0).unwrap_or(0.0) # 0.0
expect(message)
Like unwrap(), but always raises ExpectError with a custom message (preserving the original exception as __cause__).
result = Result(divide, 10, 0)
result.expect("division must succeed") # raises ExpectError("division must succeed")
API reference
| Method | Description |
|---|---|
is_ok() -> bool |
True if no error and value is set |
is_err() -> bool |
True if an error occurred or value is unset |
unwrap() -> T |
Return value or raise |
unwrap_or(default: T) -> T |
Return value or default |
expect(message: str) -> T |
Return value or raise ExpectError |
Requirements
Python 3.10 or higher. No runtime dependencies.
Project details
Release history Release notifications | RSS feed
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 better_result_py-0.1.0.tar.gz.
File metadata
- Download URL: better_result_py-0.1.0.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a76e940e45314caa62407f7abf619f5c2e3c7784f9a753b31c5dae831baf189
|
|
| MD5 |
badd8fd48fda3357193996f7e3c12f28
|
|
| BLAKE2b-256 |
0148e49a23e25157ab72f1736a5bc086839835aff85aaa290fa25e40df49c5a8
|
File details
Details for the file better_result_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: better_result_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
842fb907b0fa377482d73c16efc8297885a458f1c9d24a0ab971e236561feb18
|
|
| MD5 |
c1049b7a652c0f3271a65e3fc0ef8bcf
|
|
| BLAKE2b-256 |
65525d586269e8edbef94067d0e43004ac7289b60fb840e5876a13c1bf8102a5
|