python / python/mypy

Allow enum with zero members (rational provided)

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

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

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

説明

Feature

Just an option to bypass an error message and allow this:

class E(enum.IntEnum):
    pass

E1: E
E2: E
E3: E

Pitch

Compared to this, it has the advantage below:

class E(enum.IntEnum):
    E1 = ...
    E2 = ...
    E3 = ...

E1 = E.E1
E2 = E.E2
E3 = E.E3

When a Python API maps a C/C++ API, enum members are typically declared as int. But this is a pity, because it does not catch mixing of enum types. Also, when an enum match a C enum in an API, it is better if the class representing the type, does not have any member, because the members are aliased outside. Yes, one can write all enums instances are all of the same int type, but this is braking typing.

Also, there is an inconsistency, because in a loadable module, MyPy does not complain if an enum has no member. So why not allow it in stubs?

A practical case where the idea came to me, but I was sad to discover it does not work in stubs, while MyPy did not complain when I tried an enum with no members in a loadable module: https://github.com/AlexionSoftware/types-wxpython/issues/42#issuecomment-4962290300 . I prefer to wait for comments on this proposal, before editing the message linked, to say unfortunately it does not work, would prefer saying it just requires an option to be set.

Instead of a working clean idiom, I get this kind of error for each enum class declaration:

error: Detected enum "wx.StandardPaths.ResourceCat" in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use member = value to mark an enum member, instead of member: type [misc]

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

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

はじめの一歩

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

調査の方向性

まず、type stub 内のメンバーなし enum について報告された診断を再現し、その後、issue で説明されているロード可能モジュールの場合と比較します。enum のメンバーシップをチェックする経路と既存のエラーメッセージを確認します。通常の enum チェックを弱めることなく、意図的に指定したオプションで stub 形式を許可でき、両方のケースをカバーできれば完了です。

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

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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