python / python/mypy

Support functional API for Enum *Subclasses* too

オープン
#6,037 コメント 1 件 リアクション 20 件 担当者 0 名 GitHub で見る

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

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

説明

Tested with version 0.641.

The functional API for enums is only supported on Enum itself, and not subclasses (that do not override the relevant methods). I hit this using the OrderedEnum suggestion from the standard library documentation: https://docs.python.org/3/library/enum.html#orderedenum.

Running on:

from enum import Enum

class Foo(Enum):
    ""

Bar: type = Enum('Bar', ['X', 'Y']) # line 6
Baz: type = Foo('Baz', ['X', 'Y']) # line 7

bar: Bar = Bar.X # line 9
baz: Baz = Baz.Y # line 10

gives

asdf.py:7: error: Incompatible types in assignment (expression has type "Foo", variable has type "type")
asdf.py:7: error: Too many arguments for "Foo"
asdf.py:10: error: Invalid type "asdf.Baz"
asdf.py:10: error: "type" has no attribute "Y"

Note that 6 is deemed fine, but 7 causes complaints. I'd hope for both lines to be fine.

The even larger issue is that annotations using Baz totally fail too: Line 9 is deemed fine but 10 fails. I wouldn't mind the obscure use of the Enum subclass failing if it weren't for the fact that every annotation with my nice enum type won't work.

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

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

はじめの一歩

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

調査の方向性

issue の reproducer から始め、Enum サブクラスに対する Enum の functional API を mypy がどのように処理しているかを追跡してください。Foo('Baz', ['X', 'Y']) の推論される型と、Baz を使用するアノテーションがどのように処理されるかを確認してください。サブクラスの factory 呼び出しと Baz 型のメンバーの両方が、報告されているエラーなしに型チェックを通れば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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