python / python/mypy

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

Offen
#19,094 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-enum
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem bereitgestellten enum.Enum-Reproduzierer und vergleiche ihn mit dem int, enum.Enum-Fall, der bereits einen Fehler meldet. Verfolge, wie mypy Konstruktoraufrufe für enum-basierte Klassen prüft, und füge Tests für Foo(None) und ungültige Werte wie Foo(2) hinzu; abgeschlossen ist die Aufgabe, wenn der erwartete call-overload-Fehler gemeldet wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.