0

I have a

myenum(str, Enum):
   COLORRED = 'colorred'
   COLORBLUE = 'colorblue'

itemList = [<myenum.COLORRED,'colorred'>]

I am trying to get value from itemList 'coloured' but itemList.value gives a type check error in Python with the error message "str has no attribute value".

list(map(str,myenum)) gives key COLORRED but I need the value 'coloured'. I tried String-based enum in Python but none of them are working for me.

My enum is auto generated by openapi. below is yaml for it. I receive array of myElements and need one value in myEnum.

myElements:
  type: array
  items:
  allOf:
     - $ref: 'myEnum'

myEnum:
  type: String
  enum:
    - 'state1'
    - 'state2'

Note: Issue is with typecheck in python not iterating the values. I need enum values without using .values or .name

3 Answers3

3

Looks like you're trying to use the list's .value property, not its elements.

from enum import Enum

class Color(Enum):
    RED = "color red"
    BLUE = "color blue"

lst = [Color.RED, Color.BLUE]
expected = ['color red', 'color blue']

# This is equivalent to `list(map(lambda v: v.value, lst))`
values = [item.value for item in lst]
assert values == expected
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • I tried this way but still get type check error "str has no attribute value". can we iterate without .value and get the value instead of key? – shekhar pandey Jun 15 '23 at 05:17
  • 1
    @shekharpandey I cannot reproduce that. From the error message, it sounds like the elements of your list are strings, not your enum members. Make sure your data is what you expect it to be. – Adam Smith Jun 15 '23 at 05:51
  • You are correct, but my list is like this itemList = [] and I just want 'coloured'. Also its work fine but typecheck is giving error. – shekhar pandey Jun 15 '23 at 05:54
  • 1
    @shekharpandey yeah sorry, I can't reproduce your behavior on Python3 or Python2. You should try to figure out what your object is, because based on the error message you're incorrect about that :) – Adam Smith Jun 15 '23 at 06:03
0

Your point is not exactly clear to me, but...

If you are using Python 3.11 you can use StrEnum, so you can get the value without calling .value.

class Color(StrEnum):
    RED = 'red'
    GREEN = 'green'
    BLUE = 'blue'

print(Color.RED) #=> red

There is also IntEnum.

iGian
  • 11,023
  • 3
  • 21
  • 36
0

I believe what you are after is the values of the enum in a list. If so, this may be something you can work with.

from enum import Enum

class myenum(str, Enum):
    COLORRED = 'colorred'
    COLORBLUE = 'colorblue'

mylist = [x.value for x in myenum]
# where mylist will be ['colorred', 'colorblue']

However, if you explain how you generate your list and how you would like to use the data (perhaps with a simple example), that might help the people to understand your objectives better.

dakdad
  • 2,947
  • 2
  • 22
  • 21
  • @ dakdad Sorry for clarity, I have updated post. mylist = [x.value for x in myenum] is working but type check tool give error "str has no attribute value" – shekhar pandey Jun 15 '23 at 10:11