result.py
A Python implementation inspired by Rust's Result<T, E>, representing
operations that can either succeed (Ok) or fail (Err).
Installation
$ pip install pytresult
Key Features
-
100% Typed: Works flawlessly with
mypy— zero type errors, guaranteed. -
Well Tested: Solid 100% test coverage with
pytestso you can trust it. -
No Dependencies: Just install and go — nothing else required.
-
Rust-Style: Clean, explicit error handling inspired by Rust’s Result.
Motivation
Using Result helps avoid excessive exception handling and promotes more explicit and robust error handling.
Usage Example
from result import Result, Ok, Err
def divide(a: int, b: int) -> Result[float, str]:
if b == 0:
return Err("division by zero")
return Ok(a / b)
result = divide(10, 2)
if result.is_ok():
print("Success:", result.unwrap())
else:
print("Error:", result) # or result.unwrap_or(0)
Patterns
if let Ok(...) { }
-
Rust
fn divide(a: i32, b: i32) -> Result<f32, String> { if b == 0 { Err("division by zero".to_string()) } else { Ok(a as f32 / b as f32) } } let result = divide(10, 2); if let Ok(value) = result { println!("Success: {}", value); }
-
Python
from result import Result, Ok, Err def divide(a: int, b: int) -> Result[int, str]: if b == 0: return Err("division by zero") return Ok(a // b) result = divide(10, 2) if r := result.ok(): assert r is not None assert r == 5
if let Err(...) { }
-
Rust
fn divide(a: i32, b: i32) -> Result<f32, String> { if b == 0 { Err("division by zero".to_string()) } else { Ok(a as f32 / b as f32) } } let result = divide(10, 0); if let Err(error) = result { println!("Error: {}", error); }
-
Python
from result import Result, Ok, Err def divide(a: int, b: int) -> Result[int, str]: if b == 0: return Err("division by zero") return Ok(a // b) result = divide(10, 0) if e := result.err(): assert e is not None assert e == "division by zero"
Testing
Tests are written using pytest.
To run the tests:
poetry run pytest
Release files for pytresult 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytresult-1.0.2.tar.gz | 3.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytresult-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.5 kB
Release files / pytresult-1.0.2.tar.gz
| Download URL | pytresult-1.0.2.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ca864926f705d0ca310f63ae3b05abe86ad8b88eb21494372e6edffb78f1ff00
|
|
BLAKE2b-256 checksum How to use checksums |
6df7a284a810d070e2437a1e96d5db4adef06555152536e5e4f0b5e00004362b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure
|
Release files / pytresult-1.0.2-py3-none-any.whl
| Download URL | pytresult-1.0.2-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43a5ed2ad38664c4cd1891748f1d27aeacde2cde5e7bf470aaeb36f36084919c
|
|
BLAKE2b-256 checksum How to use checksums |
d1d3acca70faaa3507ec04da20359f82cf277cf7ebf44cbede876fa9b76d6950
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure
|