ordered-enum

raw JSON →
0.0.10 verified Mon Apr 27 auth: no python

A small library for adding total orderings to enums, providing an OrderedEnum base class that supports comparison operators (<, <=, >, >=) based on member definition order. Version 0.0.10, released in 2024, requires Python >=3.9. Maintained with infrequent releases.

pip install ordered-enum
error AttributeError: module 'ordered_enum' has no attribute 'OrderedEnum'
cause Mistyped import: using 'ordered-enum' (hyphen) or misspelling
fix
Use correct import: from ordered_enum import OrderedEnum
error TypeError: '<' not supported between instances of 'Color' and 'Color'
cause Attempting comparison between two different OrderedEnum subclasses or between OrderedEnum and a non-enum value.
fix
Ensure both operands are members of the same OrderedEnum subclass.
breaking Comparison operators (<, <=, >, >=) only work between members of the same OrderedEnum subclass. Comparing across different OrderedEnum subclasses raises TypeError.
fix Ensure comparisons only within the same enum class.
gotcha OrderedEnum does not support integer comparison with enum values. For example, Color.RED < 1 will raise TypeError. This is by design and differs from Python's standard IntEnum.
fix Use the .value attribute for numeric comparisons: Color.RED.value < 1.
deprecated The package is not actively maintained; no updates since 2024. Consider alternatives like stdlib IntEnum if integer ordering is needed.
fix For simple ordered enums, use Python's IntEnum with auto() and custom ordering, or use another library.

Define an enum with ordered comparisons based on member declaration order.

from ordered_enum import OrderedEnum

class Color(OrderedEnum):
    RED = 0
    GREEN = 1
    BLUE = 2

assert Color.RED < Color.GREEN
assert Color.BLUE > Color.RED
print("Ordering works!")