Covariant type variables should be allowed in class method
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Currently, covariant type variables are allowed in __init__ so that you can put create an immutable containers with something inside of it. See https://github.com/python/mypy/issues/2850.
It would be useful if covariant types are also allowed in class method that serve as alternative constructors. Currently mypy errors on this example, which is an extension of the other issues example:
from typing import Generic, TypeVar
T_co = TypeVar("T_co", covariant=True)
class C(Generic[T_co]):
def __init__(self, x: T_co) -> None:
...
@classmethod
def custom_init(cls, x: T_co) -> "C[T_co]":
# error: Cannot use a covariant type variable as a parameter
return cls(x)
My use case is trying to define an immutable container protocol that has a create class method, which will initialize the container with some contents, like this:
T_cov = typing.TypeVar("T_cov", covariant=True)
class ListProtocol(typing_extensions.Protocol[T_cov]):
@classmethod
def create(cls, *items: T_cov) -> "ListProtocol[T_cov]":
...
def getitem(self) -> T_cov:
...
...
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、報告にある classmethod の例で問題を再現し、mypy がメソッドパラメーター内の共変な型変数をどのように検証しているかを調べます。代替コンストラクターに意図される動作を定義し、次に受け入れられるケースを示す回帰テストを追加して、関連する型チェッカーのテストを実行します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100