backports-strenum
The `backports.strenum` library provides a backport of the `enum.StrEnum` class, which was introduced in Python 3.11. It allows developers using Python versions 3.8.6 through 3.10 to define enumerated constants that are also subclasses of `str`, behaving like both an Enum member and a string. The current version is 1.3.1, and its release cadence is tied to the need for compatibility with newer Python features.
Common errors
-
ModuleNotFoundError: No module named 'backports' OR ImportError: cannot import name 'StrEnum' from 'enum'
cause This occurs on Python versions prior to 3.11 when attempting to use `StrEnum` without `backports.strenum` installed, or when trying to import `StrEnum` directly from the `enum` module, which only includes it from Python 3.11 onwards.fixInstall `backports.strenum` using `pip install backports.strenum` and employ a conditional import statement to correctly import `StrEnum` based on the Python version: ```python import sys if sys.version_info >= (3, 11): from enum import StrEnum else: from backports.strenum import StrEnum class MyEnum(StrEnum): # ... ``` -
Dependency resolution failure or warnings when backports.strenum is included in requirements for Python 3.11+
cause The `backports.strenum` library is intended for Python versions below 3.11 because `StrEnum` is a built-in feature of the standard `enum` module starting from Python 3.11. Including `backports.strenum` as a dependency for Python 3.11 or newer environments is unnecessary and can cause conflicts with package managers.fixSpecify `backports.strenum` as a conditional dependency in your `pyproject.toml` or `setup.py` to ensure it's only installed for Python versions less than 3.11. For example, in `pyproject.toml`: ```toml [project] dependencies = [ "some-other-package", "backports.strenum; python_version < '3.11'" ] ``` -
AttributeError: 'str' object has no attribute 'name' OR AttributeError: 'str' object has no attribute 'value'
cause `StrEnum` members behave like strings; if an `StrEnum` member is implicitly or explicitly coerced into a plain string, it loses its enumeration-specific attributes like `name` or `value`, leading to this error if you try to access them on the resulting string.fixAccess the `name` or `value` attributes directly on the `StrEnum` member before any string coercion. Remember that for `StrEnum`, the member's value *is* the string itself, so `str(MyEnum.MEMBER)` or simply using `MyEnum.MEMBER` directly yields its string value. ```python from backports.strenum import StrEnum class Color(StrEnum): RED = "red" BLUE = "blue" print(Color.RED) # Outputs: red print(Color.RED.name) # Outputs: RED # print(str(Color.RED).name) # This would raise AttributeError ```
Warnings
- gotcha This library is a backport of `enum.StrEnum` from Python 3.11. If your project targets Python 3.11 or newer, you should use `from enum import StrEnum` directly from the standard library. Using `backports.strenum` on Python 3.11+ is unnecessary and may lead to import conflicts or deprecated code if not managed properly.
- gotcha `StrEnum` members behave like strings for equality checks (`==`) and can be passed where strings are expected. However, they are still distinct enum objects. Identity checks (`is`) against raw strings will fail (e.g., `MyStatus.ACTIVE is "active"` is `False`). Relying purely on string methods without converting the member to `str()` explicitly might lead to unexpected behavior in edge cases.
Install
-
pip install backports-strenum
Imports
- StrEnum
from backports.strenum import StrEnum
Quickstart
from backports.strenum import StrEnum
class MyStatus(StrEnum):
ACTIVE = "active"
INACTIVE = "inactive"
PENDING = "pending"
# Usage examples
print(MyStatus.ACTIVE) # Output: active
print(MyStatus.ACTIVE == "active") # Output: True
print(isinstance(MyStatus.INACTIVE, str)) # Output: True
def get_status_message(status: MyStatus) -> str:
if status == MyStatus.ACTIVE:
return f"Status is: {status}"
return f"Status is not active: {status}"
print(get_status_message(MyStatus.ACTIVE))
print(get_status_message(MyStatus.PENDING))