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.0.tar.gz (9.8 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.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: errorz-0.2.0.tar.gz
  • Upload date:
  • Size: 9.8 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.0.tar.gz
Algorithm Hash digest
SHA256 45d74153bb19bbe2dc50bc8bfb06cebd160e7f0849e2e26538f6ac5acb0c7a17
MD5 448c614ce5e3d2a6da8b99e718ca7f2d
BLAKE2b-256 22c32d9c46cccc1cbe67b22231b6d0f9897c7c4c2d0fa688c0b838a0ba7acb74

See more details on using hashes here.

File details

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

File metadata

  • Download URL: errorz-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72129c945eb70cc0dacb659458ac67c558f703bd5e8b07938d471377ca8c242f
MD5 27ad19cdc36392f021e644566b040fde
BLAKE2b-256 ffddd0eaf8cd83f91561221da3f1f49c53e70409a792816ac6a76e510081aad7

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