python / python/mypy

Enums with only descriptor members should not be final

Aperta
#12,494 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-descriptors topic-enum topic-runtime-semantics
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

If all members of an Enum class are descriptors, the class should still be subclassable.

Quote from the Python documentation:

The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (__str__(), __add__(), etc.), descriptors (methods are also descriptors), and variable names listed in _ignore_.

This would also match the behaviour of Pyright (1.1.234)

To Reproduce

  1. Install mypy==0.941
  2. Run mypy on this code:
import logging
from enum import Enum


class LoggedAccess:
    def __set_name__(self, owner, name):
        self.public_name = name
        self.private_name = '_' + name

    def __get__(self, obj, objtype=None):
        value = getattr(obj, self.private_name)
        logging.info('Accessing %r giving %r', self.public_name, value)
        return value

    def __set__(self, obj, value):
        logging.info('Updating %r to %r', self.public_name, value)
        setattr(obj, self.private_name, value)


class Foo(Enum):
    thing = LoggedAccess()

    
class Bar(Foo):
    x = 42

Expected Behavior

No errors - this runs just fine

Actual Behavior

 error: Cannot extend enum with existing members: "Foo"

Your Environment

  • Mypy version used: 0.941

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 riproducendo l’esempio segnalato con mypy 0.941, quindi segui l’analisi dell’enum che determina se Foo ha membri esistenti. L’issue è completata quando un enum contenente solo descrittori può essere sottoclassato senza errori, mentre gli enum con membri effettivi mantengono la restrizione esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.