python / python/mypy

Support functional API for Enum *Subclasses* too

Aperta
#6,037 1 commento 20 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-enum
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Tested with version 0.641.

The functional API for enums is only supported on Enum itself, and not subclasses (that do not override the relevant methods). I hit this using the OrderedEnum suggestion from the standard library documentation: https://docs.python.org/3/library/enum.html#orderedenum.

Running on:

from enum import Enum

class Foo(Enum):
    ""

Bar: type = Enum('Bar', ['X', 'Y']) # line 6
Baz: type = Foo('Baz', ['X', 'Y']) # line 7

bar: Bar = Bar.X # line 9
baz: Baz = Baz.Y # line 10

gives

asdf.py:7: error: Incompatible types in assignment (expression has type "Foo", variable has type "type")
asdf.py:7: error: Too many arguments for "Foo"
asdf.py:10: error: Invalid type "asdf.Baz"
asdf.py:10: error: "type" has no attribute "Y"

Note that 6 is deemed fine, but 7 causes complaints. I'd hope for both lines to be fine.

The even larger issue is that annotations using Baz totally fail too: Line 9 is deemed fine but 10 fails. I wouldn't mind the obscure use of the Enum subclass failing if it weren't for the fact that every annotation with my nice enum type won't work.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il reproducer nell’issue e segui come mypy gestisce l’API funzionale di Enum per le sottoclassi di Enum. Verifica come vengono elaborati il tipo inferito di Foo('Baz', ['X', 'Y']) e le annotazioni che usano Baz. Il lavoro è completato quando sia la chiamata alla factory della sottoclasse sia i membri tipizzati come Baz superano il controllo dei tipi senza gli errori segnalati.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.