Enum exhaustiveness checks fail with `@enum.member`-decorated members
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Functions decorated with @enum.member are as rightful enum members as simple non-callable attributes.
Beware of #18721.
To Reproduce
from enum import Enum, member
from typing import assert_never
class E1(Enum):
C = 1
def check_1(e: E1) -> None:
match e:
case E1.C:
pass
case other:
assert_never(other) # OK
if e is E1.C:
pass
else:
assert_never(e) # OK
class E2(Enum):
@member
def C() -> None: ... # E: Method must have at least one argument. Did you forget the "self" argument? [misc]
def check_2(e: E2) -> None:
match e:
case E2.C:
pass
case other:
assert_never(other) # E: Argument 1 to "assert_never" has incompatible type "E2"; expected "Never" [arg-type]
if e is E2.C:
pass
else:
assert_never(e) # E: Argument 1 to "assert_never" has incompatible type "<subclass of "enum.member[Callable[[], None]]" and "__main__.E2">"; expected "Never" [arg-type]
Expected Behavior
Green output.
Actual Behavior
main.py:22: error: Method must have at least one argument. Did you forget the "self" argument? [misc]
main.py:29: error: Argument 1 to "assert_never" has incompatible type "E2"; expected "Never" [arg-type]
main.py:34: error: Argument 1 to "assert_never" has incompatible type "<subclass of "member" and "E2">"; expected "Never" [arg-type]
Found 3 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.15.0 and current master
- Mypy command-line flags: --strict and without
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.12
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo la riproduzione fornita con Python 3.12, con e senza --strict, e confrontala con il playground collegato. Traccia il comportamento di mypy nella gestione dei membri decorati con @enum.member e dei controlli di esaustività degli enum. Il lavoro è completato quando l’esempio non produce errori, incluse entrambe le chiamate a assert_never e la definizione del membro decorato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100