Skip to main content

A generic Result[T, E] type for explicit error propagation.

Project description

fates 🔮

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.

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

fates-0.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fates-0.0.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file fates-0.0.1.tar.gz.

File metadata

  • Download URL: fates-0.0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for fates-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d6e37a2e8ae23ccc8a910be1b1817539430a0a11e1a3969613e04ec02c73f872
MD5 6af5dbffc482c286d6d272668242d663
BLAKE2b-256 052ca8237020105a25b534cd5a0f1ed9524b495d90acf82f5e2c75c6bf2342b6

See more details on using hashes here.

File details

Details for the file fates-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fates-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for fates-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7272df53a5ec85adcd0f095b53f469051379d332bd0ca2e129043f1176393a8b
MD5 f41304e29444b7f7382e24cb23f77827
BLAKE2b-256 a15ba76a8091ffa6f839af7ed0423f1c60c881673e2af8eef1b98c563bd06210

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