Enum Mixins
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file enum_mixins-0.0.2.tar.gz.
File metadata
- Download URL: enum_mixins-0.0.2.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9470e11adba630527ac88e2c13753cbcdc612d61d705559b0584a09de62093a
|
|
| MD5 |
94e59be80ee3db1e9e9d925242037b28
|
|
| BLAKE2b-256 |
54510cdea2849ae42aa5f8229b1b396c119233ac7163cc4b5c82722bb1a65c97
|
File details
Details for the file enum_mixins-0.0.2-py2.py3-none-any.whl.
File metadata
- Download URL: enum_mixins-0.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2eaf1bc5d718e64dcd2ce1b9e1d904765f0e88fd339214f159a5aadd9a6aef51
|
|
| MD5 |
92a956d73b54d4ca9e22e6ee70b9844b
|
|
| BLAKE2b-256 |
fac0b520bb12bc0434e7941842a03c75f5347535bb0b62ae2201edce5528fde5
|