Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#2,042 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
32/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu bằng cách chạy reproducer được cung cấp với Python 3.10.8, returns 0.24.0 và mypy 1.15.0, sau đó kiểm tra returns.primitives.hkt và hành vi xử lý danh sách bất biến của mypy. Công việc được xem là hoàn tất khi ba lệnh gọi được kiểm tra kiểu như minh họa và reveal_type báo cáo list[int] cho từng kết quả.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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):

Ngôn ngữ chính
Python
Star
4.4k
Fork
154
Merge trung bình
3 giờ 5 phút
Pull request đã merge (30 ngày)
22

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dry-python/returns

Tất cả issue của dry-python/returns

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.