Skip to main content

Monadic Error

by Ian Kollipara

This is a small library containing 2 monads for handling errors in Python: Attempt (Either) and Option (Maybe). These two are chosen for their usefulness in handling errors gracefully, and for the ability to slot in nicely with python. Both monads work with Python 3.10 pattern matching, and as well as MyPy exhaustive pattern matching.

All monads implement map, fmap, and unwrap_or. These all aid in their use in python. In addition there are a few utility functions for working with the objects during the execution of the program.

Installation

To install, run the following command from your terminal:

$ pip install monadic-error

Or modify your requirements.txt with the following line:

monadic-error==3.0.0

And run

$ pip install -r requirements.txt

Attempt

Attempt is the Either monad. The name was chosen to signify how it should be used. There are two constructors for this: Success and Failure. Use them as their name denotes.

from monadic_error import Attempt, Success, Failure

def div(x: int, y: int) -> Attempt[ZeroDivisionError, float]:
    if y == 0:
        return Failure(ZeroDivisionError("division by zero"))
    return Success(x / y)

# Or even shorter
from monadic_error import attempt

@attempt
def div(x: int, y: int):
    return x / y

Option

Option is the Maybe Monad. The name was chosen to signify how it should be used. There are two constructors for this: Some and Nothing. Use them as their name denotes.

from monadic_error import Option, Some, Nothing

def div(x: int, y: int) -> Option[float]:
    if y == 0:
        return Nothing()
    return Some(x / y)

# Or even shorter

from monadic_error import option

@option
def div(x: int, y: int):
    return x / y

Conversion Between Option and Attempt

There are two utility functions to convert between the two types: hush and note.

hush

Hush takes an attempt and "hushes" the error, returning an option instead

from monadic_error import hush, Success, Failure

x = Failure(1)
hush(x) # => Nothing()

y = Success(1)
hush(y) # => Some(1)

note

Note is the inverse, allowing one to state a reason for the failure, thus turning an option into an attempt.

from monadic_error import note, Some, Nothing

x = Nothing()
note(x, "This is an error") # => Failure("This is an error")

y = Some(1)
note(y, "This is an error") # => Success(1)

Metadata

Release files for monadic-error 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monadic-error 3.0.2
File Size Uploaded
monadic_error-3.0.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for monadic-error 3.0.2
File Interpreter ABI Platform
monadic_error-3.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / monadic_error-3.0.2.tar.gz

Download URL monadic_error-3.0.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8192bc6c75ccb768462a585e4d1e7583254eb1ad0281f83c1c0c098afe359525
BLAKE2b-256 checksum
How to use checksums
3858b7701c30c731dd360d0bee74930f55309feac14b35d3189265404acfb6eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure

Release files / monadic_error-3.0.2-py3-none-any.whl

Download URL monadic_error-3.0.2-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d685902fca9b279dc56decae5ed0970768d1447cce0b9bb543a8ee35412fb51
BLAKE2b-256 checksum
How to use checksums
a180b192ae192ea2bf234657c4d3e0bcd7a14aa4f2ba8d0929da0acc26f36437
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure

Release history Release notifications | RSS feed

This release

3.0.2 This release

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page