Skip to main content

flow-res

Rust 言語の Result 型に範を仰いだ、Python 3.12+ 向けの小さく型付きのエラーハンドリングライブラリです。

予期できる失敗を Result[T, E] として戻り値に表し、成功・失敗の両方を呼び出し側のコードで明示的に扱えるようにします。Python の型チェッカーやランタイムがエラー処理を強制するものではありません。未処理の Result を無視することも、unwrap() で UnwrapError として伝播させることもできます。

Err が保持する値は Exception に限りません。純粋なデータクラス、Enum、文字列などもエラー値として扱えます。unwrap() / expect() は、保持している値の型に関わらず UnwrapError を送出します。元の値は UnwrapError.error から取得でき、保持している値が BaseException の場合は __cause__ にも設定されます。

主要な特徴

  • 型チェックを CI で検証: ジェネリクスを活用し、成功値と例外型を pyright と mypy で検証します。
  • 鉄道指向プログラミング(ROP): map や and_then によるメソッドチェーンにより、宣言的なエラーハンドリングを実現します。
  • 非同期処理のネイティブサポート: @async_result デコレータを通じて、非同期処理を AwaitableResult として透過的にチェーン可能です。
  • 既存コードとの統合: @safe デコレータを用いることで、既存の例外送出型関数を Result 型へ変換できます。
  • 結果集約: combine、combine_lazy、combine_async、combine_all により、複数の処理結果を集約します。
  • 軽量設計: Python 3.13 以上ではランタイム依存がなく、プロジェクトへの導入障壁が極めて低く抑えられています。Python 3.12 では型ガード互換性のため typing-extensions を使用します。

インストール

pip install flow_res

※ Python 3.12 以上が必要です。Python 3.11 以下は対象外です。

実装ガイド

1. 基本的な定義とハンドリング

関数の戻り値に Result を指定することで、呼び出し側が成功・失敗の可能性をシグネチャから把握できます。Python 3.10 以降の構造的パターンマッチングで結果を処理できます。

from flow_res import Result, Ok, Err

def divide(a: int, b: int) -> Result[float, ValueError]:
    """2つの数値の除算を行い、結果を Result 型で返却する"""
    if b == 0:
        return Err(ValueError("Division by zero"))
    return Ok(a / b)

result = divide(10, 2)
match result:
    case Ok(value):
        print(f"Success: {value}")
    case Err(error):
        print(f"Failure: {error}")

Err には例外以外の値も格納できます。次の例では、データクラスをエラー値として使い、unwrap() が送出する UnwrapError から元の値を取り出しています。

from dataclasses import dataclass
from enum import Enum, auto

from flow_res import Err, Ok, Result, UnwrapError


class ErrorKind(Enum):
    NOT_FOUND = auto()
    INVALID = auto()


@dataclass(frozen=True, slots=True)
class AppError:
    kind: ErrorKind
    message: str


def find_user(user_id: int) -> Result[str, AppError]:
    if user_id <= 0:
        return Err(AppError(ErrorKind.INVALID, "user id must be positive"))
    return Ok(f"user-{user_id}")


result = find_user(-1)
match result:
    case Ok(name):
        print(f"Found: {name}")
    case Err(error):
        print(f"{error.kind.name}: {error.message}")

try:
    result.unwrap()
except UnwrapError as exc:
    print(isinstance(exc.error, AppError))  # True
    print(exc.__cause__)  # None (AppError is not an exception)

2. 関数型インターフェースによる連鎖処理 (Railroad-Oriented Programming)

map や and_then を用いることで、命令的な条件分岐を排除し、処理のパイプラインを構築できます。

from flow_res import Err, Ok, Result

def validate_positive(x: int) -> Result[int, ValueError]:
    if x < 0:
        return Err(ValueError("Must be positive"))
    return Ok(x)

# 依存関係のある処理の連結
result = (
    Ok(5)
    .and_then(validate_positive)
    .map(lambda x: x * 2)
    .map(lambda x: x + 3)
)
print(result.unwrap())  # 13

3. @safe デコレータによる例外のラップ

既存の例外を発生させる可能性のある関数を、低コストで Result 駆動型へ移行させます。

引数なしの @safe(および @safe())は、互換性のため、意図的にすべての Exception を Err へ変換する catch-all です。そのため、予期しないプログラミング エラーまで Err になり得ます。既存の bare @safe の動作は変更していません。 実運用では、捕捉したい対象を @safe(ValueError, TypeError) のように明示してください。 明示した型に一致しない例外はそのまま伝播します。

asyncio.CancelledError などの制御フロー用例外は Exception のサブクラスではないため、 bare @safe でも捕捉されず、キャンセルなどの制御フローを隠しません。

既存の catch-all を維持したい場合は移行不要です。捕捉範囲を狭める場合は、対象の例外を 監査したうえで @safe を @safe(ValueError, ...) に置き換えてください。

from flow_res import safe

@safe
def parse_int(s: str) -> int:
    return int(s)

# 例外は送出されず、Err として返却される
result = parse_int("not_a_number")
print(result)  # Err(error=ValueError("invalid literal for int() with base 10: 'not_a_number'"))

4. 非同期処理の統合 (@async_result)

@async_result デコレータを使用することで、非同期関数の実行結果に対しても await 前にメソッドチェーンを適用できます。 AwaitableResult は内部のコルーチンを一つだけ保持する単一消費型です。同じインスタンスを複数回 await したり、複数のチェーンへ分岐させたりせず、一つのチェーンで消費してください。複数の独立したチェーンが必要なときは、デコレート済みの関数をチェーンごとに呼び出して別インスタンスを作成します。 2回目のawait、または分岐したチェーンのうち後から消費するチェーンでは、RuntimeError("AwaitableResult can only be consumed once") が送出されます。 また、生成した AwaitableResult を await せずに破棄すると、Python が未消費コルーチンの RuntimeWarning を報告する場合があります。呼び出した非同期処理は、必ず一つのチェーンの終端で await してください。

import asyncio
from flow_res import Err, Ok, Result, async_result

@async_result
async def fetch_user(user_id: int) -> Result[dict, ValueError]:
    await asyncio.sleep(0.1)
    if user_id < 0:
        return Err(ValueError("Invalid user ID"))
    return Ok({"id": user_id, "name": f"User{user_id}"})

async def main():
    # 処理を連結した後に一括で await
    result = await (
        fetch_user(1)
        .map(lambda u: u["name"])
        .map(str.upper)
    )
    print(result.unwrap())  # USER1

asyncio.run(main())

5. 複数結果の集約ロジック (combine / combine_lazy / combine_async / combine_all)

バリデーションなど、複数の検証結果を一括で扱うためのインターフェースを提供します。

  • combine: 既に計算済みの Result のシーケンスを集約し、走査中に最初に遭遇した Err を返却する。tuple/list の入力式は combine 呼び出し前に評価済みなので、Err 以降の入力評価は止められない
  • combine_lazy: Result を返す遅延 factory を順番に呼び、最初の Err で後続 factory を呼ばずに停止する
  • combine_async: Result または Awaitable[Result] を返す遅延 factory を順番に呼び、必要なものだけ await して最初の Err で停止する(タスクを並列実行しない)
  • combine_all: すべての Err を集約し、Err(tuple[E, ...]) として全エラーを返却する(全件チェック)

combine と combine_all は、1〜10 要素の異種 tuple では各要素の型を保持します。11 要素以上の tuple と一般の Sequence も受け付けますが、その場合の成功値は tuple[Any, ...] にフォールバックします。

from flow_res import Result, combine, combine_all, combine_async, combine_lazy, Ok, Err

results = (
    Ok(1),
    Err(ValueError("error1")),
    Err(RuntimeError("error2")),
)

# 最初のエラー (error1) のみを返す
print(combine(results)) 

# 計算そのものを遅延させると、Err の後の factory は呼ばれない
print(combine_lazy((lambda: Ok(1), lambda: Err(ValueError("error1")), lambda: Ok(3))))

# すべてのエラーをタプルとして集約して返す
match combine_all(results):
    case Err(errors):
        for e in errors:
            print(f"Error: {e}")

combine_async は、同期 factory と非同期 factory を同じ iterable に混在させられます。各 factory は直列に実行され、非同期の戻り値だけが await されます。

import asyncio

from flow_res import Ok, combine_async

async def fetch_value() -> Ok[int]:
    return Ok(2)

async def main() -> None:
    result = await combine_async((lambda: Ok(1), fetch_value))
    print(result)  # Ok(value=(1, 2))

asyncio.run(main())

動作環境

  • Python バージョン: 3.12 以上(3.11 以下は対象外)
  • 型ヒント: 完全対応(Static Type Checking を推奨)

ライセンス

本プロジェクトは MIT License の下に公開されています。

協力・貢献

不具合報告や機能拡張の提案は、GitHub Issues にて承っております。 開発環境の構築、品質チェック、互換性方針、リリース手順については CONTRIBUTING.md を参照してください。

Metadata

Release files for flow-res 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flow-res 0.2.0
File Size Uploaded Provenance Actions
flow_res-0.2.0.tar.gz 75.4 kB With provenance Details for flow_res-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flow-res 0.2.0
File Interpreter ABI Platform Actions
flow_res-0.2.0-py3-none-any.whl Python 3 none any Details for flow_res-0.2.0-py3-none-any.whl

Total release size: 90.7 kB

Release files / flow_res-0.2.0.tar.gz

Download URL flow_res-0.2.0.tar.gz
Size 75.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cbb33826a318ad4d408f60e916824597fd06f77cbeda9c3639d950d77bb83049
BLAKE2b-256 checksum
How to use checksums
482d55ce42e182408a8652f07d0983fa20467067976c272170a203f246e0f5cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / flow_res-0.2.0-py3-none-any.whl

Download URL flow_res-0.2.0-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1a7552ac73334b7953d9c7b946f3c94fc45b86be0a0bb2359f4fe036b0c9165
BLAKE2b-256 checksum
How to use checksums
7546e7acd229fb59c7cb9fb8f17d85d7b0b08ed7d841eb63510bd7927c0c7254
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for flow-res 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for flow_res-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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