{"id":1394,"library":"backports-strenum","title":"backports-strenum","description":"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.","status":"active","version":"1.3.1","language":"en","source_language":"en","source_url":"https://github.com/clbarnes/backports.strenum","tags":["enum","string","backport","utility","python3"],"install":[{"cmd":"pip install backports-strenum","lang":"bash","label":"Install `backports-strenum`"}],"dependencies":[],"imports":[{"symbol":"StrEnum","correct":"from backports.strenum import StrEnum"}],"quickstart":{"code":"from backports.strenum import StrEnum\n\nclass MyStatus(StrEnum):\n    ACTIVE = \"active\"\n    INACTIVE = \"inactive\"\n    PENDING = \"pending\"\n\n# Usage examples\nprint(MyStatus.ACTIVE) # Output: active\nprint(MyStatus.ACTIVE == \"active\") # Output: True\nprint(isinstance(MyStatus.INACTIVE, str)) # Output: True\n\ndef get_status_message(status: MyStatus) -> str:\n    if status == MyStatus.ACTIVE:\n        return f\"Status is: {status}\"\n    return f\"Status is not active: {status}\"\n\nprint(get_status_message(MyStatus.ACTIVE))\nprint(get_status_message(MyStatus.PENDING))","lang":"python","description":"This example demonstrates how to define a `StrEnum` class and use its members, highlighting their string-like behavior and direct comparison capabilities."},"warnings":[{"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`).","message":"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.","severity":"gotcha","affected_versions":"All `backports-strenum` versions when used with Python 3.11+"},{"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.","message":"`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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}