Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

can't compose @future_safe coroutines that specify picky exceptions

オープン
#2,080 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
32/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
devtools

調査の方向性

まず、returns[compatible-mypy]==0.25.0 と mypy を使って提供された mre.py の例を実行し、次に future_safe と FutureResult.bind の型付けの挙動を調べます。2 つのコルーチンが型エラーなしに合成され、それぞれの ConnectionError と ZeroDivisionError のケースが分離されたままになれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Given coroutines coro1, coro2:

@future_safe(exceptions=(ConnectionError,))
async def coro1(c: int | None) -> int:
    if c is None:
        raise ConnectionError("not connected")
    await asyncio.sleep(0)  # emulate I/O
    return c


@future_safe(exceptions=(ZeroDivisionError,))
async def coro2(n: int) -> float:
    await asyncio.sleep(0)  # emulate I/O
    return 1 / n

I want to combine them via bind: coro1(c).bind(coro2). It results in type error:

$ uvx --with 'returns[compatible-mypy]==0.25.0' mypy mre.py; ./mre.py
mre.py:30: error: Argument 1 to "bind" of "FutureResult" has incompatible type "Callable[[int], FutureResult[float, ZeroDivisionError]]"; expected "Callable[[int], KindN[FutureResult[Any, Any], float, ConnectionError, Any]]"  [arg-type]

where mre.py:

#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "returns >=0.25.0",
# ]
# ///
from typing import assert_never
from returns.result import Success, Failure
from returns.io import IOSuccess, IOFailure
from returns.future import future_safe


@future_safe(exceptions=(ConnectionError,))
async def coro1(c: int | None) -> int:
    if c is None:
        raise ConnectionError("not connected")
    await asyncio.sleep(0)  # emulate I/O
    return c


@future_safe(exceptions=(ZeroDivisionError,))
async def coro2(n: int) -> float:
    await asyncio.sleep(0)  # emulate I/O
    return 1 / n


async def run() -> None:
    for c in [2, 0, None]:
        match await coro1(c).bind(coro2):
            case IOSuccess(Success(r)):
                assert r == 1 / 2, r
            case IOFailure(Failure(ZeroDivisionError(args=(msg,)))):
                assert msg == "division by zero", msg
            case IOFailure(Failure(ConnectionError(args=(msg,)))):
                assert msg == "not connected", msg
            case _ as unreachable:
                assert_never(unreachable)  # type: ignore[arg-type]


if __name__ == "__main__":
    import asyncio

    asyncio.run(run())

If picky exceptions are removed:

@future_safe
async def coro1(c: int | None) -> int: ...

@future_safe
async def coro2(n: int) -> float: ...

I get the desired behavior that coroutines are composed without type errors but these declarations catch too much.

主要言語
Python
スター
4.4k
フォーク
154
平均マージ
3時間 5分
マージ済み PR(30日)
22

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dry-python/returns のほかの issue

dry-python/returns の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。