Skip to main content

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

Project description

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.

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.2.tar.gz (7.6 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.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fates-0.0.2.tar.gz
  • Upload date:
  • Size: 7.6 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.2.tar.gz
Algorithm Hash digest
SHA256 2a361a4c8ab2449ce7fa62f2ceaa082be6a3528b14cb5c6fe66826a4e6a4781a
MD5 43be1e2f6c34560887c482b2cd81db92
BLAKE2b-256 ef8a3be12b0118a81304b97cdab61c2769df2e815bd13b3d99fd16ecfe79e298

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fates-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6dc1358a0ed8e5193d68f13e801b8a6c3f22a612db47ab0e41979018a1a41f55
MD5 c186f310d9363f2d936ca0474e84a4fe
BLAKE2b-256 c36eb6b3f2e939da275b1dd77e5e1b9eebf08c7ef1370c2b406e2a73a3e55f26

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