backports-strenum

1.3.1 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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))

view raw JSON →