Registry / type-stubs / backports-strenum

backports-strenum

JSON →
library1.3.1pypypi✓ verified 24d ago

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.

pip install backports-strenum
INSTALL
IMPORT
SIG · BACKPORTS-STRENUM
B
backports-strenum
type-stubspythonv1.3.1
Install
1.6s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.1 · 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.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.008s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

StrEnum
from backports.strenum import StrEnum

This example demonstrates how to define a `StrEnum` class and use its members, highlighting their string-like behavior and direct comparison capabilities.

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))
Debug
Known issues
gotchaThis 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.
fix
For projects targeting Python 3.11+, remove `backports-strenum` from your dependencies and update all `from backports.strenum import StrEnum` imports to `from enum import StrEnum`. Consider conditional imports for multi-version support (e.g., `try...except ImportError`).
affects: All `backports-strenum` versions when used with Python 3.11+
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.
fix
Always use `==` for value comparison. If strictly string-only functionality is required, explicitly convert the `StrEnum` member to a string using `str(MyStatus.MEMBER)` before performing operations that require a raw string.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'backports' OR ImportError: cannot import name 'StrEnum' from 'enum'
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.
fix
Install `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+
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.
fix
Specify `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'
`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.
fix
Access 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
```
Upgrade
Version history
1.3.1latest on PyPI · released Dec 9, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
56 hits · last 30 days
node
48
OpenAI (training)
1
Resources
backports-strenum — pip install backports-strenum · libregistry