11.6. Enum Check

EnumCheck contains the options used by the verify decorator to ensure various constraints; failed constraints result in a ValueError.

11.6.1. UNIQUE

Ensure that each value has only one name:

>>> from enum import Enum, verify, UNIQUE
>>> @verify(UNIQUE)
... class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...     CRIMSON = 1
Traceback (most recent call last):
ValueError: aliases found in <enum 'Color'>: CRIMSON -> RED

11.6.2. CONTINUOUS

  • Designed to work with integer-valued members

Ensure that there are no missing values between the lowest-valued member and the highest-valued member:

>>> from enum import Enum, verify, CONTINUOUS
>>> @verify(CONTINUOUS)
... class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 5
Traceback (most recent call last):
ValueError: invalid enum 'Color': missing values 3, 4

11.6.3. NAMED_FLAGS

  • Designed to work with integer-valued members

Ensure that any flag groups/masks contain only named flags -- useful when values are specified instead of being generated by auto()

>>> from enum import Flag, verify, NAMED_FLAGS
>>> @verify(NAMED_FLAGS)
... class Color(Flag):
...     RED = 1
...     GREEN = 2
...     BLUE = 4
...     WHITE = 15
...     NEON = 31
Traceback (most recent call last):
ValueError: invalid Flag 'Color': aliases WHITE and NEON are missing combined values of 0x18 [use enum.show_flag_values(value) for details]

11.6.4. Property

  • enum.property

  • Since Python: 3.11

A decorator similar to the built-in property, but specifically for enumerations. It allows member attributes to have the same names as members themselves.

Note

the property and the member must be defined in separate classes; for example, the value and name attributes are defined in the Enum class, and Enum subclasses can define members with the names value and name.

11.6.5. Unique

  • enum.unique

A class decorator specifically for enumerations. It searches an enumeration's __members__, gathering any aliases it finds; if any are found ValueError is raised with the details:

>>> from enum import Enum, unique
>>> @unique
... class Mistake(Enum):
...     ONE = 1
...     TWO = 2
...     THREE = 3
...     FOUR = 3
...
Traceback (most recent call last):
ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE

11.6.6. Verify

  • enum.verify

  • Since Python: 3.11

A class decorator specifically for enumerations. Members from EnumCheck are used to specify which constraints should be checked on the decorated enumeration.

11.6.7. Member

  • enum.member

  • Since Python: 3.11

A decorator for use in enums: its target will become a member.

11.6.8. Nonmember

  • enum.nonmember

  • Since Python: 3.11

A decorator for use in enums: its target will not become a member.

11.6.9. Global Enum

  • enum.global_enum

  • Since Python: 3.11

A decorator to change the str() and repr of an enum to show its members as belonging to the module instead of its class. Should only be used when the enum members are exported to the module global namespace (see re.RegexFlag for an example).

11.6.10. Show Flag Values

  • enum.show_flag_values(value)

  • Since Python: 3.11

Return a list of all power-of-two integers contained in a flag value.