Plugin interface for type narrowing on `==` comparisons
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
説明
Feature
I'm proposing a new plugin interface for type narrowing on == comparisons.
Pitch
Take the following code:
from enum import IntEnum
class MyIntEnum(IntEnum):
A = 1
B = 2
m: MyIntEnum
if m == 1:
pass
else:
reveal_type(m)
This should reveal MyIntEnum.B, but mypy can't know that because it has no idea about how the __eq__ method on MyIntEnum works.
A plugin interface together with a builtin plugin could fix that.
mypy would walk through the MyIntEnum MRO until it encounters a class that defines __eq__ and then look for a plugin able to handle this class.
The plugin matching the class will be called and can return two types. One narrowing for if the condition matches and one for if it doesn't.
This would also be useful for checking match statements (#10191). Currently I have to special case enums there, as match always compares emums with ==, while mypy only narrows types if enums are compared with is.
It would probably also make it possible to get rid of the special casing of a == type(A) and replace it with a plugin.
I would like to first hear some opinions about this before I start implementing it.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、mypy に既存する plugin インターフェースと、== と is における enum の絞り込みの現在の動作を確認します。次に、#10191 で参照されている match 文のケースを調べ、マッチする比較とマッチしない比較に対して plugin がどのような絞り込み型を返すかを定義します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 20/100