Imperative-style railway-oriented programming in Python, including fully-typed errors. Supports sync and async functions and methods.
Install pyrop
:
pip install pyrop
A simple example:
from pyrop import EitherMonad, Left, catch, monadic
catcher = catch[ValueError | TypeError]()
@catcher
def func_with_error() -> None:
raise ValueError("This is an error")
@catcher
def success_func() -> int:
return 1
@monadic
def func(do: EitherMonad[ValueError | TypeError]) -> int:
value = do << success_func()
print(f"Value is {value}")
do << func_with_error()
print("This will not execute")
return value
res = func()
assert isinstance(res, Left)
assert isinstance(res.value, ValueError)
try:
func().get()
except ValueError as e:
print(f"Caught error: {e}")
assert func().map_left(lambda e: "Error occurred") == Left("Error occurred")
assert func().get_or_else(1) == 1
See the documentation here.
This project is currently in early-stage development. There may be breaking changes often. While the major version is 0, minor version upgrades will often have breaking changes.
See the development guide for development details.
Created by Nick DeRobertis. MIT License.