EnumCheckMixin
- Description: Provides the
obj_to_enum
method for Enum objects, allowing for the conversion of various types into the corresponding enum values.
The design concept of this method is to prevent users from using incorrect enum values by enforcing type constraints while also avoiding frustration when a matching enum value cannot be found.
Thus, the obj_to_enum
method is available to convert different types of enum values into the correct enum type.
Users can query enum values using strings, enum values, or integers.
-
Example
from enum import IntEnum
from capybara import EnumCheckMixin
class Color(EnumCheckMixin, IntEnum):
RED = 1
GREEN = 2
BLUE = 3
color = Color.obj_to_enum('GREEN')
print(color) # Color.GREEN
color = Color.obj_to_enum(Color.RED)
print(color) # Color.RED
color = Color.obj_to_enum(3)
print(color) # Color.BLUE