python / python/mypy

Attempted to reuse member name in Enum definition

Aperta
#17,234 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

I'm trying to follow the dynamic generation of Enum values example as described in the Enum documentation, and I'm getting this from mypy:

example.py:23: error: Attempted to reuse member name "i" in Enum definition "Slot"  [misc]

Now, I am trying to reuse the member name; in contrast to the Enum example, I have multiple loops that generate several sets of Enum values, and I am reusing the same loop variable across all of them (see the example below). mypy doesn't complain if I only have one loop in my Enum.

But considering I've already declared that member to be ignored in _ignore_, I don't see why I should use multiple distinct loop variables for all my loops, if all of them are going to be ignored by Enum plumbing anyway.

Moreover, the loop variable is used in a loop anyway; it will definitely get reassigned multiple values even during the first loop execution. I don't see how having multiple loops makes any difference to having a single loop.

To Reproduce

Run source through mypy:

import enum


class MyEnum(enum.StrEnum):
    _ignore_ = 'MyEnum i'
    MyEnum = vars()

    for i in range(8):
        MyEnum[f'SOMETHING_{i}'] = f'Something{i}'

    for i in range(4):
        MyEnum[f'ANOTHER_{i}'] = f'Another{i}'

Expected Behavior

mypy should not complain about this code.

Actual Behavior

mypy complains about the member being reused at the line of the second loop.

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
files = src
mypy_path = stubs
strict = True
install_types = True
  • Python version used: 3.11.9

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

Riproduci la diagnostica con l'esempio fornito usando mypy 1.10.0 e conferma che il secondo ciclo attiva l'avviso di riutilizzo, mentre un singolo ciclo non lo fa. Traccia l'analisi della definizione di Enum che gestisce ignore e le variabili del ciclo, quindi aggiungi o aggiorna un test di regressione in modo che l'esempio non segnali più un errore.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.