Standard library map on bound function
まだ誰も着手していません。
評価
調査の方向性
報告されている test.py の再現から始め、returns.pointfree.bind、ResultE、Python 組み込みの map の相互作用に焦点を当てます。mypy が推論した型を、動作する for ループおよび明示的な g の代替案と比較します。完了の条件は、元の map(bind(g), map(f, range(3))) の例がエラーなしで型チェックを通ることです。
索引モデルが issue の本文から書いたものです。
説明
Bug report
Mypy does not appear to correctly handle the standard library map function over an iterable of containers that returns an iterable of containers, when the function being mapped over the data has had returns.pointfree.bind applied to it. The same code when used without map (by explicit use of a for-loop) or a bind (by explicitly handling a container input) will not result in an error.
What's wrong
Code to reproduce issue
from typing import Iterable
from returns.pointfree import bind
from returns.result import ResultE
from returns.result import safe
@safe
def f(x: int) -> str:
return str(x)
@safe
def g(x: str) -> int:
return int(x)
def h(xs: Iterable[ResultE[int]]) -> None:
for x in xs:
print(x.unwrap())
if __name__ == "__main__":
h(map(bind(g), map(f, range(3))))
Mypy output
$ mypy test.py
test.py:24:11: error: Argument 1 to "map" has incompatible type "Kinded[Callable[[KindN[Result[Any, Any], str, Exception, NoReturn]], KindN[Result[Any, Any], int, Exception, NoReturn]]]"; expected "Callable[[Result[str, Exception]], Res
ult[int, Exception]]" [arg-type]
test.py:24:11: note: "Kinded[Callable[[KindN[Result[Any, Any], str, Exception, NoReturn]], KindN[Result[Any, Any], int, Exception, NoReturn]]].__call__" has type "Callable[[KindN[Result[Any, Any], str, Exception, NoReturn]], KindN[Resul
t[Any, Any], int, Exception, NoReturn]]"
Found 1 error in 1 file (checked 1 source file)
Python output
$ python test.py
0
1
2
How it should be
When the h function is replaced with a for-loop in __main__ no issues are reported by Mypy and the code executes correctly:
if __name__ == "__main__":
for x in map(f, range(3)):
print(x.bind(g).unwrap())
Can also fix the issue by swapping out the g function:
def g(x: ResultE[str]) -> ResultE[int]:
return x.map(int)
<snip>
if __name__ == "__main__":
h(map(g, map(f, range(3))))
System information
pythonversion: 3.8.6returnsversion: 0.15.0mypyversion: 0.800 (also observed in v0.782 due toclassesslibrary dependency)
- 主要言語
- 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
-
essnmx good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
-
[Feature] 奇物选择添加优先级 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
syfoud/Simulated_Scepter#174 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Giskard-AI/giskard-oss#2840 · コメント 1 件 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success オープンarea: repo bug perceived difficulty: 2
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
yeti-platform/yeti#1380 ·