Standard library map on bound function
还没有人认领这个 Issue。
评估
调研方向
从报告的 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 分钟
- 30 天内合并 PR
- 22
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dry-python/returns 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 48/100
dry-python/returns#2394 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 52/100
dry-python/returns#2365 ·
-
难度 5/5 一周以上 新手友好度 25/100
dry-python/returns#2355 · 1 条评论 ·
-
bug
难度 4/5 3-5 天 新手友好度 45/100
dry-python/returns#2295 · 4 条评论 · 1 个 reaction ·
-
bug
难度 4/5 3-5 天 新手友好度 45/100
dry-python/returns#2253 · 2 条评论 ·
查看 dry-python/returns 的全部 Issue
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 70/100
canonical/paas-charm#368 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
tech debt
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 1/5 1 小时以内 新手友好度 90/100
StevenBlack/hosts#3256 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
qualcomm/qai-appbuilder#275 ·