StrEnum
StrEnum is a Python library that provides an `Enum` class that inherits from `str`, allowing enumeration members to behave directly like strings. This is particularly useful for scenarios involving APIs, JSON serialization, and generally replacing 'magic strings' with type-safe, readable constants. The library is actively maintained, with version 0.4.15 being the current release, and follows a frequent, minor-version release cadence.
Warnings
- breaking Support for Python 3.6 was dropped in version 0.4.9. Users on older Python versions will need to pin `strenum<0.4.9` or upgrade their Python environment.
- gotcha Python 3.11 introduced `enum.StrEnum` in the standard library. The `strenum` library is *not* a drop-in replacement for the standard library's `StrEnum`, especially regarding the default behavior of `auto()`. The `strenum.StrEnum` preserves the member name as its value when `auto()` is used (e.g., `MY_VALUE` becomes `'MY_VALUE'`), while the standard library's `enum.StrEnum` defaults to lowercasing (`MY_VALUE` becomes `'my_value'`).
- gotcha When using the case-converting `StrEnum` subclasses (e.g., `CamelCaseStrEnum`, `KebabCaseStrEnum`), the automatic name conversion to value only applies when `auto()` is used. Manually assigned values will be used exactly as provided, without any case transformation.
- gotcha While `strenum` members compare equal to their underlying string values (e.g., `MyEnum.FOO == "FOO"` returns `True`), relying on this for critical logic can sometimes lead to subtle bugs or typos. It's generally safer and more type-explicit to compare enum members directly (e.g., `MyEnum.FOO is MyEnum.FOO` or `MyEnum.FOO == another_enum_member`).
- gotcha In some parts of the Python standard library, checks for `type(unknown) == str` are performed instead of `isinstance(unknown, str)`. In these specific cases, a `strenum` member might not be recognized as a plain string. To ensure compatibility, you might need to explicitly cast the enum member to `str` (e.g., `str(MyStrEnum.MY_MEMBER)`).
Install
-
pip install strenum
Imports
- StrEnum
from strenum import StrEnum
- LowercaseStrEnum
from strenum import LowercaseStrEnum
- CamelCaseStrEnum
from strenum import CamelCaseStrEnum
- auto
from enum import auto
Quickstart
from enum import auto
from strenum import StrEnum, LowercaseStrEnum
class HttpMethod(StrEnum):
GET = auto() # Value will be 'GET'
POST = 'post_value' # Explicitly assigned string value
PUT = auto()
class FileExtension(LowercaseStrEnum):
TXT = auto() # Value will be 'txt'
PDF = auto()
assert HttpMethod.GET == 'GET'
assert HttpMethod.POST == 'post_value'
assert HttpMethod.PUT == 'PUT'
assert FileExtension.TXT == 'txt'
assert FileExtension.PDF == 'pdf'
print(f"HTTP Method: {HttpMethod.GET}")
print(f"File Type: {FileExtension.PDF}")
# Enums compare equal to their string value
assert HttpMethod.GET == "GET"
# However, it's generally recommended to compare enum to enum for type safety
if HttpMethod.GET is HttpMethod.GET:
print("GET method detected.")