mypy reports an error when passing enum values as arguments
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
Starting with https://github.com/python/mypy/commit/1a04c073260c13107abfab6ae0ff4c8cc214eb54 @sterliakov enum values assigned to functions are typed as Callables. This seems to make mypy report an error when passing enum values to functions that expect the enum.
To Reproduce
from enum import Enum
import operator
class Op(Enum):
ADD = operator.add
SUB = operator.sub
MUL = operator.mul
def takes_op(op: Op, x: int, y: int):
return op.value(x, y)
print(takes_op(Op.ADD, 2, 2))
Expected Behavior
No error
Actual Behavior
/Users/psawicki/test.py:12: error: Argument 1 to "takes_op" has incompatible type "Callable[[Any, Any], Any]"; expected "Op" [arg-type]
Your Environment
- Mypy version used: mypy 1.20.0+dev.1a04c073260c13107abfab6ae0ff4c8cc214eb54 and up
- Mypy command-line flags:
mypy ~/test.py - Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.13.5
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
Esegui l'esempio Python minimo dell'issue con mypy 1.20.0+dev.1a04c073260c13107abfab6ae0ff4c8cc214eb54 e traccia come vengono tipizzati i membri dell'enum a cui sono assegnati valori chiamabili. Aggiungi un test di regressione per passare Op.ADD a takes_op e conferma che mypy non segnali alcun errore di tipo dell'argomento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 58/100