A very simple, fully typed Rust-like Result type for Python 3.
Project description
simple-result
A very simple, fully typed Rust-like Result type for Python 3.
If you need a Result type only for checking if an operation succeeded or failed, and don't need to perform special actions like chaining operations, mapping, etc., then this library is for you.
If you are looking for a more feature-rich Result type, check out rustedpy/result.
Installation
# Using pip
pip install simple-result
# Using Poetry
poetry add simple-result
# Using uv
uv add simple-result
Usage
import random
from simple_result import Err, Ok, Result
def fetch_data() -> Result[str, ConnectionError]:
fetched = random.choice([True, False])
if fetched:
return Ok('Data fetched!')
return Err(ConnectionError('Error fetching data!'))
Check if the result is Ok or Err using type narrowing:
if res := fetch_data():
print(res.value) # "Data fetched!"
print(res.error) # None
else:
print(res.error) # "Error fetching data!"
print(res.value) # None
Or using match:
match fetch_data():
case Ok(data):
print(data) # "Data fetched!"
case Err(error):
print(error) # "Error fetching data!"
match fetch_data():
case Ok(data):
print(data) # "Data fetched!"
case Err(error, code):
print(error, code) # "Error fetching data! 1"
Call .unwrap_value() to get the value or raise an UnwrapError if the result
is Err:
from simple_result import UnwrapError
try:
res = Err(ConnectionError('Error fetching data!'))
print(res.unwrap_value())
except UnwrapError as exc:
print(str(exc)) # called `Result.unwrap_value()` on an `Err` value
print(exc.result.error) # "Error fetching data!"
Call .unwrap_error() to get the error or raise an UnwrapError if the result
is Ok:
try:
res = Ok('Data fetched!')
print(res.unwrap_error())
except UnwrapError as exc:
print(str(exc)) # called `Result.unwrap_error()` on an `Ok` value
print(exc.result.value) # "Data fetched!"
Compare results:
assert Ok(1) == Ok(1)
assert Ok(1) != Ok(2)
exc = ValueError('error')
assert Err(exc) == Err(exc)
other_exc = ValueError('other error')
assert Err(exc) != Err(other_exc)
Check if the result is Ok or Err using isinstance:
from simple_result import ResultOption
res = fetch_data()
assert isinstance(res, ResultOption)
assert isinstance(res, (Ok, Err))
Contributing
See the contribution guidelines.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
If you find this project useful, give it a ⭐ on GitHub!
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 simple_result-0.2.0.tar.gz.
File metadata
- Download URL: simple_result-0.2.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3468547e36f8079c3b0e5325b3b2018e3c32fd61de9d0f2cd8d36f852d761d71
|
|
| MD5 |
cfb459a3083db4c4d1237b14d3b93cd5
|
|
| BLAKE2b-256 |
b3f7e1ea503bfd0b456f2c1df5369d4070700e9e2ca1451f6ccd2998f4d6b70c
|
Provenance
The following attestation bundles were made for simple_result-0.2.0.tar.gz:
Publisher:
publish.yml on daireto/simple-result
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_result-0.2.0.tar.gz -
Subject digest:
3468547e36f8079c3b0e5325b3b2018e3c32fd61de9d0f2cd8d36f852d761d71 - Sigstore transparency entry: 767711630
- Sigstore integration time:
-
Permalink:
daireto/simple-result@95f3cfeb69361d02a42fa1d6254efa819e17f26f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/daireto
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@95f3cfeb69361d02a42fa1d6254efa819e17f26f -
Trigger Event:
push
-
Statement type:
File details
Details for the file simple_result-0.2.0-py3-none-any.whl.
File metadata
- Download URL: simple_result-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d15e7a64f0c6bcb4ea83f6cd055cbfb29786649994428c7c64c4e0ddc96d28ea
|
|
| MD5 |
b3b86edf7e61501d89c4050810b5d8cf
|
|
| BLAKE2b-256 |
1bffff55a6e2ad190558fe25c933103c565914eb780f767a315613844a16561b
|
Provenance
The following attestation bundles were made for simple_result-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on daireto/simple-result
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_result-0.2.0-py3-none-any.whl -
Subject digest:
d15e7a64f0c6bcb4ea83f6cd055cbfb29786649994428c7c64c4e0ddc96d28ea - Sigstore transparency entry: 767711637
- Sigstore integration time:
-
Permalink:
daireto/simple-result@95f3cfeb69361d02a42fa1d6254efa819e17f26f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/daireto
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@95f3cfeb69361d02a42fa1d6254efa819e17f26f -
Trigger Event:
push
-
Statement type: