Skip to main content

Represent fallible computations as values, instead of using exceptions.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

errorz-0.2.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

errorz-0.2.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file errorz-0.2.1.tar.gz.

File metadata

  • Download URL: errorz-0.2.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for errorz-0.2.1.tar.gz
Algorithm Hash digest
SHA256 10039963ddf388c25f3ec1a39d6a09751fedf00288887c89f13f4bbc66646ebc
MD5 40db0fb160cd0d168fcbf5a88fe3b1c0
BLAKE2b-256 f27b8648112468769d30202de2360d6d3da240329ab4030cc7c559c0b44600df

See more details on using hashes here.

File details

Details for the file errorz-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: errorz-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for errorz-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84a3c3fba98cf9dab75f88c00a561f51a3b245f27e0cc77910288a13c3c57f9d
MD5 38574f527d1d7620094ca373c07267b8
BLAKE2b-256 6ac4afa19777295a63a22ad3c4704a9967193f1f55d9d734cfb28952855d0a9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page