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

Kinded type behaving differently from non-kinded type in an invariant wrapper

未关闭
#2,042 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

首先使用 Python 3.10.8、returns 0.24.0 和 mypy 1.15.0 运行提供的复现程序,然后检查 returns.primitives.hkt 以及 mypy 处理不变列表时的行为。当这三个调用如图所示通过类型检查,并且 reveal_type 对每个结果都报告 list[int] 时,即表示完成。

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

描述

bug

Hi Nikita, long time! Thanks for the interesting library bringing Haskell-like FP patterns to Python.

Bug report

What's wrong

I'm trying to extract the type T from a Kind1 type (say, Foo[T]) that is wrapped in an invariant type, e.g., list[Foo[T]]. However, that's leading to some unexpected behavior.

Repro:

from typing import Any, TypeVar

from returns.primitives import hkt

T = TypeVar("T")


class Foo(hkt.SupportsKind1["Foo[Any]", T]):
    pass


# These are just helper functions to extract the type `T`.
def expect_list_foo(foo_list: list[Foo[T]]) -> list[T]:
    return []


@hkt.kinded
def expect_list_foo__kinded(foo_list: list[hkt.Kind1[Foo[Any], T]]) -> list[T]:
    return []


@hkt.kinded
def expect_tuple_foo__kinded(foo_tuple: tuple[hkt.Kind1[Foo[Any], T], str]) -> list[T]:
    return []


def inferred_types(foo_list: list[Foo[int]], foo_tuple: tuple[Foo[int], str]) -> None:
    x = expect_list_foo(foo_list)

    y = expect_list_foo__kinded(foo_list)  # <-- Problem

    z = expect_tuple_foo__kinded(foo_tuple)

    reveal_type(x)
    reveal_type(y)
    reveal_type(z)

    print(x, y, z)

How is that should be

I expected all three calls to work and return list[int].

Actual output:

30: error: Argument 1 to "expect_list_foo__kinded" has incompatible type "list[Foo[int]]"; expected "list[KindN[Foo[Any], int, Any, Any]]"  [arg-type]
30: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
30: note: Consider using "Sequence" instead, which is covariant
34: note: Revealed type is "builtins.list[builtins.int]"
35: note: Revealed type is "builtins.list[builtins.int]"
36: note: Revealed type is "builtins.list[builtins.int]"
Found 1 error in 1 file (checked 1 source file)

Seems like list[Foo[int]] is not treated as compatible with list[hkt.Kind1[Foo[Any], T]]. tuple[Foo[int], str] is treated as compatible with tuple[hkt.Kind1[Foo[Any], T], str]. So, it seems like this might be due to the invariance of list.

Is this expected behavior? How can I get this to work?

System information

  • python version: 3.10.8

  • returns version: 0.24.0

  • mypy version: 1.15.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 摘要。