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

1
  • Please update your question with a complete snippet of code that we can run to see the problem ourselves. Commented Jun 15, 2023 at 18:41

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

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?
@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.
You are correct, but my list is like this itemList = [<myenum.COLORRED, 'colorred'>] and I just want 'coloured'. Also its work fine but typecheck is giving error.
@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 :)
1

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.

Comments

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.

1 Comment

@ 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"
0

Use magic method :

from enum import StrEnum, auto


class Color(StrEnum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()

    @classmethod
    def __iter__(cls):
        return iter(cls.__members__.values())

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.