Enum Mixins
enum_mixins is a small Python package of mixins for adding practical behavior
to enum and flag classes, especially aenum.Flag.
The package is pre-alpha and intentionally narrow: it provides runtime validation helpers, type-friendly flag iteration, non-null flag behavior, and custom names for combined flag values.
What It Provides
IterableFlagMixin: keepsaenum.Flagvalues iterable while giving the iterator a useful type hint, and includes a helper for OR-ing a non-empty list of flags into one combined flag value.ValidatableEnumMixin: addsvalidate(key), which accepts only values that are instances of the enum class.ValidatableFlagMixin: addsvalidate_single_value(key), which rejects combined flag values such asColor.RED | Color.BLUE.NonNullableFlagMixin: rejects zero/nullaenum.Flagvalues produced at runtime, such asColor(0)or operations whose result is zero.CustomNameSeparatorFlagMixin: customizes the separator used in generated names for combinedaenum.Flagvalues, for exampleRED_BLUEinstead ofRED|BLUE.
Installation
pip install enum_mixins
The package requires Python 3.8 or newer and depends on aenum and
typing-extensions.
For local development:
pip install -r requirements_dev.txt
pip install -e .
make test
make lint
Usage
Place mixins before aenum.Flag in the class bases:
import aenum
from enum_mixins import (
CustomNameSeparatorFlagMixin,
IterableFlagMixin,
NonNullableFlagMixin,
ValidatableFlagMixin,
)
class Color(
IterableFlagMixin,
ValidatableFlagMixin,
NonNullableFlagMixin,
CustomNameSeparatorFlagMixin,
aenum.Flag,
):
__default_separator__ = "_"
RED = aenum.auto()
BLUE = aenum.auto()
GREEN = aenum.auto()
MIXED = RED | GREEN
Color.validate(Color.RED)
Color.validate(Color.RED | Color.BLUE)
Color.validate_single_value(Color.RED)
# Color.validate_single_value(Color.RED | Color.BLUE) raises ValueError
(Color.RED | Color.BLUE).name
# "RED_BLUE"
list(Color.RED | Color.BLUE)
# [Color.RED, Color.BLUE]
Color(0)
# raises ValueError
CustomNameSeparatorFlagMixin only changes generated combined names. If a
combined value is explicitly named in the enum, such as MIXED = RED | GREEN,
the defined enum name is preserved.
Behavior Notes
The validation mixins work at runtime: validate() checks that the value is an
instance of the enum class, and validate_single_value() additionally checks
that exactly one flag is set. They do not parse strings or coerce external
values into enum members.
NonNullableFlagMixin is intended for aenum.Flag. The current tests cover
runtime zero-value rejection, but class-definition-time rejection of an explicit
zero member is marked as expected-fail.
IterableFlagMixin.combine_flags_list_into_single_multiflag_value() uses
functools.reduce, so it expects at least one flag in the list.
Project Status
Version 0.0.1 is marked as pre-alpha in the package metadata. The project is
MIT licensed.
Changelog
0.0.1
- First release on PyPI.
Release files for enum-mixins 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| enum_mixins-0.0.2.tar.gz | 11.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| enum_mixins-0.0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size:16.5 kB
Release files / enum_mixins-0.0.2.tar.gz
| Download URL | enum_mixins-0.0.2.tar.gz |
|---|---|
| Size | 11.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f9470e11adba630527ac88e2c13753cbcdc612d61d705559b0584a09de62093a
|
|
BLAKE2b-256 checksum How to use checksums |
54510cdea2849ae42aa5f8229b1b396c119233ac7163cc4b5c82722bb1a65c97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / enum_mixins-0.0.2-py2.py3-none-any.whl
| Download URL | enum_mixins-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
2eaf1bc5d718e64dcd2ce1b9e1d904765f0e88fd339214f159a5aadd9a6aef51
|
|
BLAKE2b-256 checksum How to use checksums |
fac0b520bb12bc0434e7941842a03c75f5347535bb0b62ae2201edce5528fde5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|