A practical Result type for Python — Ok, Err, and pattern matching.
Project description
crashbytes-result
A practical Result type for Python — Ok, Err, and pattern matching.
Install
pip install crashbytes-result
Usage
from crashbytes_result import Ok, Err, Result, safe
def divide(a: float, b: float) -> Result[float, str]:
if b == 0:
return Err("division by zero")
return Ok(a / b)
result = divide(10, 3)
match result:
case Ok(value):
print(f"Result: {value}")
case Err(error):
print(f"Error: {error}")
# Or use .match()
msg = result.match(
ok=lambda v: f"Got {v:.2f}",
err=lambda e: f"Failed: {e}",
)
# @safe decorator
@safe
def parse_int(s: str) -> int:
return int(s)
parse_int("42") # Ok(42)
parse_int("abc") # Err(ValueError(...))
API
| Method | Ok | Err |
|---|---|---|
.unwrap() |
Returns value | Raises UnwrapError |
.unwrap_or(default) |
Returns value | Returns default |
.unwrap_err() |
Raises UnwrapError |
Returns error |
.map(fn) |
Ok(fn(value)) |
self |
.map_err(fn) |
self |
Err(fn(error)) |
.bind(fn) |
fn(value) |
self |
.match(ok, err) |
ok(value) |
err(error) |
.is_ok |
True |
False |
.is_err |
False |
True |
License
MIT
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 crashbytes_result-1.1.1.tar.gz.
File metadata
- Download URL: crashbytes_result-1.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b44877c7adca6822cadf09edf4e63eeb3f32b11d4b9ff10af51807b9f283a1f4
|
|
| MD5 |
b27b0a4fa5ad75c425561ad7b6742545
|
|
| BLAKE2b-256 |
9da129ac4531fd763448f6acb9e9b003b99792e0350e1d01d05fd4a4d22345ed
|
Provenance
The following attestation bundles were made for crashbytes_result-1.1.1.tar.gz:
Publisher:
publish.yml on CrashBytes/crashbytes-result
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
crashbytes_result-1.1.1.tar.gz -
Subject digest:
b44877c7adca6822cadf09edf4e63eeb3f32b11d4b9ff10af51807b9f283a1f4 - Sigstore transparency entry: 1774099673
- Sigstore integration time:
-
Permalink:
CrashBytes/crashbytes-result@58c48ea674802c03ebfeeb97a1981dd5b31f7ad2 -
Branch / Tag:
refs/tags/v1.1.1 - Owner: https://github.com/CrashBytes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@58c48ea674802c03ebfeeb97a1981dd5b31f7ad2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file crashbytes_result-1.1.1-py3-none-any.whl.
File metadata
- Download URL: crashbytes_result-1.1.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a9724a853864a2b5c0390e85b194969c42b398da7ccee1b491aa131eeaa6f9e
|
|
| MD5 |
08846d48d01981c80d9f40b660c4ff1f
|
|
| BLAKE2b-256 |
45446427079fd410c58c720768e03949e81586d0711f8f66a73f1d74e1d060b1
|
Provenance
The following attestation bundles were made for crashbytes_result-1.1.1-py3-none-any.whl:
Publisher:
publish.yml on CrashBytes/crashbytes-result
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
crashbytes_result-1.1.1-py3-none-any.whl -
Subject digest:
8a9724a853864a2b5c0390e85b194969c42b398da7ccee1b491aa131eeaa6f9e - Sigstore transparency entry: 1774100136
- Sigstore integration time:
-
Permalink:
CrashBytes/crashbytes-result@58c48ea674802c03ebfeeb97a1981dd5b31f7ad2 -
Branch / Tag:
refs/tags/v1.1.1 - Owner: https://github.com/CrashBytes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@58c48ea674802c03ebfeeb97a1981dd5b31f7ad2 -
Trigger Event:
push
-
Statement type: