Type annotations for Enum value

2020-05-29 16:23发布

I have this piece of code:

import enum


class Color(enum.Enum):
    RED = '1'
    BLUE = '2'
    GREEN = '3'


def get_color_return_something(some_color):
    pass

How do I properly add type annotations to the some_color varaible in this function, if I suppose to receive a value from the Color enum (for example: Color.RED)?

2条回答
你好瞎i
2楼-- · 2020-05-29 17:20
def get_color_return_something(some_color: Color):
    pass
查看更多
贼婆χ
3楼-- · 2020-05-29 17:24

Type hinting the Color class should work:

def get_color_return_something(some_color: Color):
    print(some_color.value)
查看更多
登录 后发表回答