Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Standard library map on bound function

未关闭
#807 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
tooling

调研方向

从报告的 test.py 复现开始,重点关注 returns.pointfree.bind、ResultE 与 Python 内置 map 之间的交互。将 mypy 推断出的类型与可正常工作的 for 循环和显式的 g 替代方案进行比较;完成的标准是原始的 map(bind(g), map(f, range(3))) 示例能够无错误地通过类型检查。

由索引模型根据 Issue 内容生成。

描述

bug

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

  • python version: 3.8.6
  • returns version: 0.15.0
  • mypy version: 0.800 (also observed in v0.782 due to classess library dependency)
主要语言
Python
星标
4.4k
派生
154
平均合并
3 小时 5 分钟
30 天内合并 PR
22

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dry-python/returns 的其他 Issue

查看 dry-python/returns 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。