graphql-python/graphene

`graphene.Enum` returns wrong `name` if members with different names have the same value

zedzior opened this issue · 2 comments

  • What is the current behavior?
import graphene

class MyEnum(graphene.Enum):
    IN_STOCK = "AVAILABLE"
    AVAILABLE = "AVAILABLE"
    OUT_OF_STOCK = "OUT_OF_STOCK"
>>>
>>> from graphene_enum_issue import MyEnum
>>> MyEnum.IN_STOCK.name
'IN_STOCK'
>>> MyEnum.AVAILABLE.name
'IN_STOCK'
>>> MyEnum.OUT_OF_STOCK.name
'OUT_OF_STOCK'
>>>
  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via
    a github repo, https://repl.it or similar.
  1. create some graphene.Enum object
  2. add two members with different name, but the same value
  3. try to access both member's name
  • What is the expected behavior?
>>>
>>> from graphene_enum_issue import MyEnum
>>> MyEnum.IN_STOCK.name
'IN_STOCK'
>>> MyEnum.AVAILABLE.name
'AVAILABLE'
>>> MyEnum.OUT_OF_STOCK.name
'OUT_OF_STOCK'
>>>
  • What is the motivation / use case for changing the behavior?
    When we update a name of some graphql schema's field, with exactly the same behaviour, we want to add descriptions:
  1. when IN_STOCK will be deprecated
  2. when AVAILABLE was added

It results both descriptions point to deprecated info

  • Please tell us about your environment:

    • Version: 2.1.9
    • Platform: macOS

@zedzior this happens with the built in Python enums as well:

from enum import Enum

class MyEnum(Enum):
    IN_STOCK = "AVAILABLE"
    AVAILABLE = "AVAILABLE"
    OUT_OF_STOCK = "OUT_OF_STOCK"
>>> MyEnum.IN_STOCK
<MyEnum.IN_STOCK: 'AVAILABLE'>
>>> MyEnum.IN_STOCK.name
'IN_STOCK'
>>> MyEnum.AVAILABLE.name
'IN_STOCK'
>>> MyEnum.OUT_OF_STOCK.name
'OUT_OF_STOCK'

@jkimbo Was not aware, thanks for quick reply!