Kinded type behaving differently from non-kinded type in an invariant wrapper
まだ誰も着手していません。
評価
調査の方向性
まず、Python 3.10.8、returns 0.24.0、mypy 1.15.0 で提供された再現コードを実行し、次に returns.primitives.hkt と mypy の不変リストの処理の挙動を調査します。3 つの呼び出しが示されているとおりに型チェックを通り、それぞれの結果について reveal_type が list[int] を報告すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
-
pythonversion: 3.10.8 -
returnsversion: 0.24.0 -
mypyversion: 1.15.0 -
hypothesisversion (if any): -
pytestversion (if any):
- 主要言語
- Python
- スター
- 4.4k
- フォーク
- 154
- 平均マージ
- 3時間 5分
- マージ済み PR(30日)
- 22
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dry-python/returns のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
dry-python/returns#2394 · リアクション 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
dry-python/returns#2365 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
dry-python/returns#2355 · コメント 1 件 ·
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
dry-python/returns#2295 · コメント 4 件 · リアクション 1 件 ·
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
dry-python/returns#2253 · コメント 2 件 ·
dry-python/returns の issue をすべて見る
似ている issue
-
bug confirmed issue
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
open-webui/open-webui#30750 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 90/100