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

Generic Point Free Function Returns Unexpected Type

未关闭
#1,453 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

重现 test_generic2 的 mypy 输出,重点关注 bind_result 入口点以及报告中显示的泛型 Callable 签名。将推断出的类型与预期的 IOResult[T4, Exception] 结果进行比较,并确认当示例通过类型检查且没有嵌套的 IOResult 错误时,该问题已得到解决。

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

描述

bug

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

  • python version: 3.9.12

  • returns version: 0.19.0

  • mypy version: 0.95.0

  • hypothesis version (if any):

  • pytest version (if any):

主要语言
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 摘要。