Enum Properties¶
Add properties to Python enumeration values in a simple declarative syntax.
enum-properties is a lightweight extension to
Python’s enum.Enum
class. Example:
import typing as t
from enum_properties import EnumProperties
from enum import auto
class Color(EnumProperties):
rgb: t.Tuple[int, int, int]
hex: str
# name value rgb hex
RED = auto(), (1, 0, 0), 'ff0000'
GREEN = auto(), (0, 1, 0), '00ff00'
BLUE = auto(), (0, 0, 1), '0000ff'
# the type hints on the Enum class become properties on
# each value, matching the order in which they are specified
assert Color.RED.rgb == (1, 0, 0)
assert Color.GREEN.rgb == (0, 1, 0)
assert Color.BLUE.rgb == (0, 0, 1)
assert Color.RED.hex == 'ff0000'
assert Color.GREEN.hex == '00ff00'
assert Color.BLUE.hex == '0000ff'
Properties may also be symmetrically mapped to enumeration values using Symmetric type annotations:
import typing as t
from enum_properties import EnumProperties, Symmetric
from enum import auto
class Color(EnumProperties):
rgb: t.Annotated[t.Tuple[int, int, int], Symmetric()]
hex: t.Annotated[str, Symmetric(case_fold=True)]
RED = auto(), (1, 0, 0), 'ff0000'
GREEN = auto(), (0, 1, 0), '00ff00'
BLUE = auto(), (0, 0, 1), '0000ff'
# Enumeration instances may be instantiated from any Symmetric property
# values. Use case_fold for case insensitive matching
assert Color((1, 0, 0)) is Color.RED
assert Color((0, 1, 0)) is Color.GREEN
assert Color((0, 0, 1)) is Color.BLUE
assert Color('ff0000') is Color.RED
assert Color('FF0000') is Color.RED # case_fold makes mapping case insensitive
assert Color('00ff00') is Color.GREEN
assert Color('00FF00') is Color.GREEN
assert Color('0000ff') is Color.BLUE
assert Color('0000FF') is Color.BLUE
assert Color.RED.hex == 'ff0000'
Member functions may also be specialized to each enumeration value, using the @specialize
decorator:
from enum_properties import EnumProperties as Enum, specialize
class SpecializedEnum(Enum):
ONE = 1
TWO = 2
THREE = 3
@specialize(ONE)
def method(self):
return 'method_one()'
@specialize(TWO)
def method(self):
return 'method_two()'
@specialize(THREE)
def method(self):
return 'method_three()'
assert SpecializedEnum.ONE.method() == 'method_one()'
assert SpecializedEnum.TWO.method() == 'method_two()'
assert SpecializedEnum.THREE.method() == 'method_three()'
Please report bugs and discuss features on the issues page.
Contributions are encouraged!
Full documentation at read the docs.
Installation¶
pip install enum-properties
Contents:
- How To
- Add Properties to an Enum
- Get Enums from their properties
- Handle Symmetric Overloads
- Mark
name
as Symmetric - Mark @properties as Symmetric
- Specializing Member Functions
- Flags
- Use Nested Classes as Enums
- What about dataclass Enums?
- Get members and aliases
- Define hash equivalent enums
- Use the legacy (1.x) API
- Tutorial
- Reference
- Change Log
- v2.3.0 (2025-03-29)
- v2.2.5 (2025-03-22)
- v2.2.4 (2025-03-17)
- v2.2.3 (2025-03-14)
- v2.2.2 (2025-03-07)
- v2.2.1 (2025-03-07)
- v2.2.0 (2025-03-07)
- v2.1.0 (2025-03-06)
- v2.0.1 (2024-09-02)
- v2.0.0 (2024-09-02)
- v1.8.1 (2024-08-29)
- v1.8.0 (2024-08-26)
- v1.7.0 (2023-10-02)
- v1.6.0 (2023-08-22)
- v1.5.2 (2023-05-06)
- v1.5.1 (2023-04-17)
- v1.5.0 (2023-04-17)
- v1.4.0 (2023-04-08)
- v1.3.3 (2023-02-15)
- v1.3.2 (2023-02-15)
- v1.3.1 (2022-10-25)
- v1.3.0 (2022-10-25)
- v1.2.2 (2022-10-25)
- v1.2.1 (2022-10-25)
- v1.2.0 (2022-08-17)
- v1.1.1 (2022-07-24)
- v1.1.0 (2022-07-23)
- v1.0.2 (2022-07-19)
- v1.0.1 (2022-07-18)
- v1.0.0 (2022-07-18)