graphql-python / graphql-python/graphene

Enum equality comparison is inconsistent

Aperta
#1,524 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
🐛 bug
Lingua principale
Python
Stelle
8.2k
Fork
818
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

## What is the expected behavior?

I expect the following code to always succeed.

It succeeds in simple scripts. But in a larger code base it fails:
```python
class Kind(graphene.Enum):
INTERMEDIATE_FAVORABLE = "intermediate_favorable"

assert Kind.INTERMEDIATE_FAVORABLE == "intermediate_favorable"
```

## Version
- Version: graphene-3.3
- Platform: cpython 3.11.4, macos ventura 13.5.1 (M1)

## Other information

The cause of the bug is here:
https://github.com/graphql-python/graphene/blob/master/graphene/types/enum.py#L12

Equivalent strings are not guaranteed to have the same id.

```python
def eq_enum(self, other):
if isinstance(other, self.__class__):
return self is other
return self.value is other
```

There's a one-line bug fix:
```python
def eq_enum(self, other):
if isinstance(other, self.__class__):
return self is other
return self.value == other
```

And I suppose maybe an optimization:
```python
def eq_enum(self, other):
if isinstance(other, self.__class__):
return self is other
return self.value is other or self.value == other
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Inizia in graphene/types/enum.py, nell’implementazione di eq_enum identificata nell’issue, e confronta i relativi controlli di identità con l’assertion segnalata. Verifica che i valori enum equivalenti vengano confrontati in modo coerente con la stringa corrispondente, quindi conferma che la riproduzione fornita abbia esito positivo con le versioni di Python e Graphene indicate.

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

Valutazione

Stack tecnologico
python
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.