Registry / serialization / strenum

strenum

JSON →
library0.4.15pypypi✓ verified 24d ago

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.

pip install strenum
INSTALL
IMPORT
SIG · STRENUM
S
strenum
serializationpythonv0.4.15
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.15 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.010s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

StrEnum
from strenum import StrEnum
LowercaseStrEnum
from strenum import LowercaseStrEnum
CamelCaseStrEnum
from strenum import CamelCaseStrEnum
auto
from enum import auto
The 'auto' function is part of Python's standard 'enum' module, not 'strenum'.

This quickstart demonstrates defining a basic `StrEnum` where `auto()` uses the member name directly, and a `LowercaseStrEnum` which converts `auto()` values to lowercase. It also shows manual string assignment and how `StrEnum` members behave like strings, including comparison.

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.")
Debug
Known issues
breakingSupport 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.
fix
Upgrade Python to 3.7+ or pin `strenum` to a version less than 0.4.9 (`pip install "strenum<0.4.9"`).
affects: >=0.4.9
gotchaPython 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'`).
fix
Be explicit when using `auto()`: use `strenum.StrEnum` for literal name values, or `strenum.LowercaseStrEnum`, `strenum.CamelCaseStrEnum`, etc., for specific casing. If migrating to `enum.StrEnum` (stdlib), be aware of its default lowercasing behavior for `auto()` or define a custom `_generate_next_value_`.
affects: All versions, when considering migration to Python 3.11+ stdlib `StrEnum`
gotchaWhen 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.
fix
Always use `auto()` if you expect the name conversion to apply to the member's value, or explicitly assign the desired string value if it should not be converted.
affects: All versions with case-converting StrEnum subclasses
gotchaWhile `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`).
fix
Prefer comparing enum members to other enum members. If comparing to a string is necessary, consider its implications for type safety and potential errors.
affects: All versions
gotchaIn 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)`).
fix
If encountering unexpected type mismatches in string-sensitive standard library functions, explicitly cast the `strenum` member to `str`.
affects: All versions
Upgrade
Version history
0.4.15latest on PyPI · released Jun 29, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
16
Meta
2
Amazon
1
OpenAI (training)
1
Resources