result-py

A functional programming library for Python that brings type-safe error handling through the Either monad pattern.

Why result-py?

Traditional Python error handling uses exceptions, which are:

  • Invisible: Nothing in the type signature tells you a function can fail

  • Unstructured: Any function can throw any exception at any time

  • Hard to compose: Try/except blocks interrupt your code flow

With Either, errors become values that are:

  • Explicit: The return type tells you exactly what can go wrong

  • Type-checked: Your IDE and type checker help you handle all cases

  • Composable: Chain operations naturally with .pipe()

Quick Example

from result_py import Either

def divide(a: float, b: float) -> Either[str, float]:
    if b == 0:
        return Either.left("Division by zero!")
    return Either.right(a / b)

result = (
    Either.right(100.0)
    .pipe(lambda x: divide(x, 4))   # Right(25.0)
    .pipe(lambda x: divide(x, 5))   # Right(5.0)
    .match(
        left=lambda err: f"Error: {err}",
        right=lambda val: f"Result: {val}"
    )
)
print(result)  # "Result: 5.0"

Indices and tables