Skip to main content

fates 🔮

PyPI Version Code Quality Check

A robust, fully-typed, and async-ready Result pattern implementation for Python.

fates brings expressive, functional error handling to Python, completely removing the need for defensive try/except blocks. It helps you build predictable pipelines with absolute type safety for both synchronous and asynchronous operations.


✨ Features

  • 🛡️ 100% Type Safe: Full mypy / pyright compliance using generic protocols and TypeVar covariance.
  • ⚡ Async Native: First-class support for asynchronous mapping and monadic binding.
  • 🧩 Zero Dependencies: Lightweight and clean, relying only on standard library primitives (and typing_extensions for older Python versions).

🚀 Installation

pip install fates

📖 Quick Start

Synchronous Usage

from fates import Result, Ok, Err

def divide(a: int, b: int) -> Result[float, str]:
    if b == 0:
        return Err("Cannot divide by zero")
    return Ok(a / b)

# Monadic binding and mapping
result = (
    divide(10, 2)
    .map(lambda x: x * 2)
    .unwrap_or(0.0)
)
print(result)  # Output: 10.0

Asynchronous Pipelines

fates shines when working with async databases or HTTP clients. Use amap and abind to pipe async operations seamlessly.

import asyncio
from fates import Ok, Err, Result

async def fetch_user_id(username: str) -> Result[int, str]:
    # Simulating async DB call
    await asyncio.sleep(0.1)
    return Ok(42) if username == "admin" else Err("User not found")

async def get_user_role_async(user_id: int) -> Result[str, str]:
    await asyncio.sleep(0.1)
    return Ok("superuser")

async def main():
    # Chain async functions using abind
    pipeline = await fetch_user_id("admin").abind(get_user_role_async)
    
    print(pipeline)  # Output: Ok('superuser')
    print(pipeline.unwrap())  # Output: superuser

asyncio.run(main())

🛠️ API Reference

Extracting Values

  • .unwrap() — Returns the success value or raises an UnwrapError.
  • .unwrap_or(default) — Returns the success value or a fallback value.
  • .unwrap_err() — Returns the error value or raises an UnwrapError.
  • .expect(note) — Returns the success value or crashes with a custom message.

Transforming Results

  • .map(mapper) — Transforms the success value inside Ok.
  • .map_err(mapper) — Transforms the error value inside Err.
  • .bind(binder) — Monadic bind. Chains another operation that returns a Result.
  • .catch(binder) — Recovers from an error by returning an alternative Result.
  • .resolve(mapper) — Merges both paths into a single success-type value.

Async Operations

  • .amap(async_mapper) — Asynchronously transforms the success value.
  • .amap_err(async_mapper) — Asynchronously transforms the error value.
  • .abind(async_binder) — Asynchronously chains another operation returning a Result.

⚖️ License

This project is licensed under the MIT License.

Release files for fates 0.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 fates 0.0.2
File Size Uploaded
fates-0.0.2.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fates 0.0.2
File Interpreter ABI Platform
fates-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / fates-0.0.2.tar.gz

Download URL fates-0.0.2.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2a361a4c8ab2449ce7fa62f2ceaa082be6a3528b14cb5c6fe66826a4e6a4781a
BLAKE2b-256 checksum
How to use checksums
ef8a3be12b0118a81304b97cdab61c2769df2e815bd13b3d99fd16ecfe79e298
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","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}

Release files / fates-0.0.2-py3-none-any.whl

Download URL fates-0.0.2-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6dc1358a0ed8e5193d68f13e801b8a6c3f22a612db47ab0e41979018a1a41f55
BLAKE2b-256 checksum
How to use checksums
c36eb6b3f2e939da275b1dd77e5e1b9eebf08c7ef1370c2b406e2a73a3e55f26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","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}

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

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