python / python/mypy

There should be an inverse of `Type`

オープン
#8,103 コメント 5 件 リアクション 9 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Specifically, it would be nice if there was a keyword Instance such that Instance[Type[_T]] is equivalent to _T

I'm thinking about this mostly in the context of working with metaclasses.

Suppose I have a class with a class method that creates an instance of the class written so it can be reused by subclasses. This can be typed in the following way:

_ThingInstance = TypeVar('_ThingInstance', bound='Thing')

class Thing():

    @classmethod
    def make_new(cls: Type[_ThingInstance]) -> _ThingInstance:
        return cls()

And this is automatically usable by subclasses.

What I want is an Instance type such that I can correctly type the same logic defined within a metaclass like so:

_ThingClass = TypeVar('_ThingClass', bound='MetaThing')

class MetaThing(type):

  def make_new(cls: _ThingClass) -> Instance[_ThingClass]:
      return cls()

As far as I can tell, there's no way to correctly annotate this

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提案されている Instance[_ThingClass] アノテーションから始め、示されている Type[_ThingInstance] パターンと比較します。メタクラスメソッドに対して逆方向がどのように振る舞うべきかを定義し、型チェックのカバレッジで例を検証します。要求されたアノテーションが意図した推論済みのインスタンスタイプで受け入れられれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
28/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。