Errorz
Python officially handle errors using exceptions. This library provides a
lightweight approach inspired by the Result type used in Rust and other
functional programming languages: Result is an union between a success value
and an error value.
The main advantage over exceptions is that they are expressed as values in the type system. This allows for better static guarantees and make code more explicit and easier to compose, since results are just like any other value that can be passed around and manipulated without cluncky try/catch blocks. Exceptions also break control flow and can be easily forgotten and lead to crashes and brittle refactorings.
The cannonical way to implement this pattern is to use a tagged union of Ok[T]
and Err[E]. This is what Rust and Haskell do and there are some Python
libraries that follow this approach, e.g.,
returns. However, Python don't have native
tagged unions and using them often feels unergnomic and non-Pythonic. This
library explores a more lightweight approach that defines Result[T, E] = T | Error[E], where Error[E] is a special abstract wrapper around an error value.
This is simular to the approach we used in the
optionz library for nullables, where it was
more lightweight and in line with established Python idioms.
Installation
Install Errorz using pip/uv/poetry whatever you like. For example:
pip install errorz
Errorz consists of a single file, so you can also just copy the __init__.py
file to your project as errorz.py and import it from there. Our special Error
wrapper is defined as a protocol and has some special structure to ensure that
different copies of the vendorized lib can coexist without conflicts.
Usage
Import err and use the functions as needed.
import errorz as rz
rz.unwrap(42) # 42
rz.unwrap(rz.err("error")) # raises ValueError
Documentation
The documentation is available at https://optionz.rtfd.io/ and includes more examples and explanations of the functions provided by the library.
License
Optionz is licensed under the MIT License. See LICENSE for more details.
Metadata
Release files for errorz 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| errorz-0.2.1.tar.gz | 11.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| errorz-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.7 kB
Release files / errorz-0.2.1.tar.gz
| Download URL | errorz-0.2.1.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
10039963ddf388c25f3ec1a39d6a09751fedf00288887c89f13f4bbc66646ebc
|
|
BLAKE2b-256 checksum How to use checksums |
f27b8648112468769d30202de2360d6d3da240329ab4030cc7c559c0b44600df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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}
|
Release files / errorz-0.2.1-py3-none-any.whl
| Download URL | errorz-0.2.1-py3-none-any.whl |
|---|---|
| Size | 12.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
84a3c3fba98cf9dab75f88c00a561f51a3b245f27e0cc77910288a13c3c57f9d
|
|
BLAKE2b-256 checksum How to use checksums |
6ac4afa19777295a63a22ad3c4704a9967193f1f55d9d734cfb28952855d0a9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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}
|