Enum `@member` and `nonmember` mishandled in `Literal` types
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
typing.Literal is supposed to accept enum members and not non-members (such as methods or explicit nonmember attributes). mypy fails to enforce the latter and support the former.
To Reproduce
from enum import Enum, member, nonmember
from typing import Literal
class E(Enum):
A = 1
@member
def B() -> None: ...
C = member(lambda: None)
D = nonmember(1)
def meth(self) -> None: ...
a: Literal[E.A] # OK, true negative
b: Literal[E.B] # False positive: Parameter 1 of Literal[...] is invalid [valid-type]
c: Literal[E.C] # OK, true negative
d: Literal[E.D] # False negative - `nonmember` shouldn't be allowed
meth: Literal[E.meth] # OK, true positive: Parameter 1 of Literal[...] is invalid [valid-type]
Expected Behavior
- The following should be accepted in Literal:
E.A,E.B,E.C - The following should be rejected in Literal:
E.D,E.meth
Actual Behavior
(line 7 is a different issue reported separately)
main.py:7: error: Method must have at least one argument. Did you forget the "self" argument? [misc]
main.py:14: error: Parameter 1 of Literal[...] is invalid [valid-type]
main.py:17: error: Parameter 1 of Literal[...] is invalid [valid-type]
Your Environment
- Mypy version used: 1.15.0 and current master
- Mypy command-line flags: N/A
- 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 di Python 3.12 con mypy 1.15.0 o il master corrente; quindi traccia la validazione di Literal e la gestione di enum-member coinvolte in queste diagnostiche. Il lavoro è completato quando E.A, E.B ed E.C vengono accettati, mentre E.D ed E.meth vengono rifiutati.
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