Enum Tools
Enum Tools is a Python library that expands upon Python's standard `enum` module. It provides utilities such as a Sphinx extension (`autoenum`) for improved Enum documentation, decorators (`document_enum`) to automatically add docstrings to Enum members from source code comments, and a collection of custom Enum classes (`custom_enums`) with enhanced functionality like `StrEnum`, `AutoNumberEnum`, and `OrderedEnum`. The library is currently at version 0.13.0 and maintains an active release schedule.
Warnings
- gotcha When using `document_enum`, ensure that docstrings are consistent. If more than one docstring format is found for an enum member, a `MultipleDocstringsWarning` will be emitted.
- breaking Changing the integer values of standard enum members, even those extended by `enum-tools`, can be a breaking change if those values are persisted or relied upon by external systems. This is a general Python `enum` concern.
- gotcha The `enum_tools.autoenum` Sphinx extension has additional dependencies (`sphinx`, `sphinx-jinja2-compat`, `sphinx-toolbox`) that are not installed by default with `pip install enum-tools`.
Install
-
pip install enum-tools -
pip install enum-tools[sphinx]
Imports
- document_enum
from enum_tools.documentation import document_enum
- StrEnum
from enum_tools.custom_enums import StrEnum
- autoenum
from enum_tools.autoenum import autoenum
Quickstart
from enum import Enum, auto
from enum_tools.documentation import document_enum
from enum_tools.custom_enums import StrEnum
@document_enum
class Status(Enum):
NEW = auto() # doc: New item status
IN_PROGRESS = auto() # doc: Item is being processed
COMPLETED = auto() # doc: Item processing finished
class Color(StrEnum):
RED = auto()
GREEN = auto()
BLUE = auto()
print(f"Status.NEW name: {Status.NEW.name}, value: {Status.NEW.value}, doc: {Status.NEW.__doc__}")
print(f"Status.IN_PROGRESS name: {Status.IN_PROGRESS.name}, value: {Status.IN_PROGRESS.value}, doc: {Status.IN_PROGRESS.__doc__}")
print(f"Color.RED: {Color.RED}, type: {type(Color.RED)}")
print(f"Color.GREEN == 'GREEN': {Color.GREEN == 'GREEN'}")