mirror of
https://github.com/unmojang/meta.git
synced 2025-09-29 22:23:29 -04:00
33 lines
899 B
Python
33 lines
899 B
Python
import enum
|
|
|
|
|
|
class StrEnum(str, enum.Enum):
|
|
"""
|
|
StrEnum is a Python ``enum.Enum`` that inherits from ``str``. The default
|
|
``auto()`` behavior uses the member name as its value.
|
|
|
|
Example usage::
|
|
|
|
class Example(StrEnum):
|
|
UPPER_CASE = auto()
|
|
lower_case = auto()
|
|
MixedCase = auto()
|
|
|
|
assert Example.UPPER_CASE == "UPPER_CASE"
|
|
assert Example.lower_case == "lower_case"
|
|
assert Example.MixedCase == "MixedCase"
|
|
"""
|
|
|
|
def __new__(cls, value, *args, **kwargs):
|
|
if not isinstance(value, (str, enum.auto)):
|
|
raise TypeError(
|
|
f"Values of StrEnums must be strings: {value!r} is a {type(value)}"
|
|
)
|
|
return super().__new__(cls, value, *args, **kwargs)
|
|
|
|
def __str__(self):
|
|
return str(self.value)
|
|
|
|
def _generate_next_value_(name, *_):
|
|
return name
|