python / python/mypy

Fails to flag `None` passed to constructor of `enum.Enum`-based classes

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

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

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

説明

Bug Report

Mypy fails to flag None passed to constructor of enum.Enum-based classes.

To Reproduce

import enum


class Foo(enum.Enum):
    one = 1


Foo(None)

Note that changing to class Foo(int, enum.Enum) does make mypy flag the error. However, the two aren't equivalent as the latter lets you do Foo.one + 1 while the former requires Foo.one.value + 1.

Edit: In fact, in both forms it fails to flag invalid arguments such as Foo(2). I do realize this is a slightly different case, which is why I'm focusing on None which has different behavior between the two.

Expected Behavior

I would expect mypy to flag the last line as an error:

error: No overload variant of "Foo" matches argument type "None"  [call-overload]

Actual Behavior

Mypy reports no errors.

Your Environment

Tested on

  • Mypy 1.13.0 and 1.15.0
  • CPython 3.11.10

Used mypy --strict file.py and just mypy file.py.

Contents of mypy.ini (I actually use a pyproject.toml file, but have tested with only this ini-file):

[mypy]
check_untyped_defs = true
disallow_any_generics = false
disallow_incomplete_defs = false
disallow_subclassing_any = false
disallow_untyped_calls = false
disallow_untyped_decorators = true
disallow_untyped_defs = false
enable_error_code = truthy-bool, truthy-iterable
extra_checks = true
ignore_missing_imports = false
local_partial_types = true
no_implicit_optional = true
no_implicit_reexport = false
no_namespace_packages = true
python_version = 3.11
strict_equality = true
warn_redundant_casts = true
warn_return_any = false
warn_unused_configs = true
warn_unused_ignores = true

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

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

はじめの一歩

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

調査の方向性

提供されている enum.Enum の再現コードから始め、すでにエラーを報告している int, enum.Enum のケースと比較します。mypy が enum ベースのクラスのコンストラクター呼び出しをどのようにチェックしているかを追跡し、Foo(None) と Foo(2) のような無効な値のテストカバレッジを追加します。期待される call-overload エラーが報告されれば完了です。

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

評価

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

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

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