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.1.0.tar.gz (9.0 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.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: errorz-0.1.0.tar.gz
  • Upload date:
  • Size: 9.0 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.1.0.tar.gz
Algorithm Hash digest
SHA256 568a6698ce5a2d10412847f54f7dfe580073b4917928e401194b093882ddeebc
MD5 17159bf2072a166b46536169c7b432de
BLAKE2b-256 85568daef896a12546b9a704a9be92aae838d9c890cbe53ae50e3e4da7bd0bec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: errorz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba32756fd3c847eaa9edcbeae9d637d1cd7f649a47c294200d49daa2f308f899
MD5 2720b7aacf2e6e8c50e54557a77219be
BLAKE2b-256 bbad54336c226e1c9c8fe203a976363253b66603593a0c5673719f2d2450feb2

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