Generic Point Free Function Returns Unexpected Type
まだ誰も着手していません。
評価
調査の方向性
test_generic2 の mypy の出力を再現し、エントリポイント bind_result とレポートに示されているジェネリックな Callable シグネチャに焦点を当てます。推論された型を、期待される IOResult[T4, Exception] 型の結果と比較し、ネストされた IOResult エラーなしで例の型チェックが成功すれば問題が解決されたことを確認します。
索引モデルが issue の本文から書いたものです。
説明
Bug report
What's wrong
Trying to compose a generic function that converts between different container types fails to type check correctly when using generics. Hopefully I am just using this wrong.
Code:
from typing import Callable, TypeVar
from returns.io import IOResultE
from returns.pointfree import bind_result
from returns.result import ResultE
from typing_extensions import reveal_type
T1 = TypeVar("T1")
T2 = TypeVar("T2")
T3 = TypeVar("T3")
T4 = TypeVar("T4")
# this works correctly
def test(
i: int,
f1: Callable[[int], str],
f2: Callable[[str], IOResultE[int]],
f3: Callable[[int], ResultE[str]],
) -> IOResultE[str]:
f1_res = f1(i)
f2_res = f2(f1_res)
f3_res = bind_result(f3)(f2_res)
reveal_type(f3_res)
return f3_res
# this passes typecheck with returns.io.IOResult[T3`-3, builtins.Exception]
def test_generic(
i: T1,
f1: Callable[[T1], IOResultE[T2]],
f2: Callable[[T2], ResultE[T3]],
) -> IOResultE[T3]:
f1_res = f1(i)
f2_res = bind_result(f2)(f1_res)
reveal_type(f2_res)
return f2_res
# this fails typecheck with returns.io.IOResult[returns.io.IOResult[Any, Any], builtins.Exception]
def test_generic2(
i: T1,
f1: Callable[[T1], T2],
f2: Callable[[T2], IOResultE[T3]],
f3: Callable[[T3], ResultE[T4]],
) -> IOResultE[T4]:
f1_res = f1(i)
f2_res = f2(f1_res)
f3_res = bind_result(f3)(f2_res)
reveal_type(f3_res)
return f3_res
Running mypy:
$ python3 -m mypy test.py
test.py:23: note: Revealed type is "returns.io.IOResult[builtins.str, builtins.Exception]"
test.py:34: note: Revealed type is "returns.io.IOResult[T3`-3, builtins.Exception]"
test.py:47: note: Revealed type is "returns.io.IOResult[returns.io.IOResult[Any, Any], builtins.Exception]"
test.py:48: error: Incompatible return value type (got "IOResult[IOResult[Any, Any], Exception]", expected "IOResult[T4, Exception]")
Found 1 error in 1 file (checked 1 source file)
How is that should be
I would have expected the return type for test_generic2 to be returns.io.IOResult[T4`-3, builtins.Exception]
System information
-
pythonversion: 3.9.12 -
returnsversion: 0.19.0 -
mypyversion: 0.95.0 -
hypothesisversion (if any): -
pytestversion (if any):
- 主要言語
- Python
- スター
- 4.4k
- フォーク
- 154
- 平均マージ
- 3時間 5分
- マージ済み PR(30日)
- 22
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dry-python/returns のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
dry-python/returns#2394 · リアクション 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
dry-python/returns#2365 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
dry-python/returns#2355 · コメント 1 件 ·
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
dry-python/returns#2295 · コメント 4 件 · リアクション 1 件 ·
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
dry-python/returns#2253 · コメント 2 件 ·
dry-python/returns の issue をすべて見る
似ている issue
-
area: harness bug status: needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Human-Agent-Society/reef#625 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 80/100
learningequality/kolibri#15351 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
Name consistency オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
eellak/triplestore#65 · コメント 1 件 ·