Skip to main content

An alternative to the built-in Python `enum` implementation

Project description

Data Enums

Build Status Coverage: 10%0 PyPI version

An alternative to the built-in Python enum implementation. Supports Python 3.11+, including free-threaded Python (3.14t).

Data enums allow you to:

  • Define enum members with associated data using type hints
  • Enforce unique constraints on attributes
  • Look up members by name or unique attribute values
  • Filter members by any attribute
  • Use pure enums without any data attributes

Usage

Install via PyPI:

pip install data-enum

Basic example

from typing import Annotated

from data_enum import DataEnum, UNIQUE, UniqueTogether

class Currency(DataEnum):
    __members__ = ("USD", "EUR", "GBP")

    symbol: str
    name: str
    code: Annotated[str, UNIQUE]

Currency.USD = Currency(symbol="$", name="US Dollar", code="USD")
Currency.EUR = Currency(symbol="€", name="Euro", code="EUR")
Currency.GBP = Currency(symbol="£", name="Pound Sterling", code="GBP")

Access data on members

Currency.USD.name      # 'US Dollar'
Currency.EUR.symbol    # '€'
str(Currency.USD)      # 'USD'
repr(Currency.USD)     # 'Currency.USD'

Look up members by name

Currency.get("USD")                          # Currency.USD
Currency.get("MISSING", default=None)        # None
Currency.get("MISSING", default=Currency.USD)  # Currency.USD

Look up by unique attribute

Currency.get(code="EUR")                          # Currency.EUR
Currency.get(code="MISSING", default=Currency.USD)  # Currency.USD

Filter by any attribute

Currency.filter(symbol="$")  # frozenset({Currency.USD})
Currency.filter(symbol="₿")  # frozenset()

Compare and hash

Currency.USD == Currency.EUR  # False
Currency.USD == Currency.USD  # True

{Currency.USD, Currency.EUR}  # works in sets
{Currency.USD: "dollar"}      # works as dict keys

Default values

Use standard Python default syntax to make attributes optional:

class Currency(DataEnum):
    __members__ = ("USD", "EUR", "GBP")

    symbol: str
    name: str
    code: Annotated[str, UNIQUE]
    active: bool = True

Currency.USD = Currency(symbol="$", name="US Dollar", code="USD")           # active=True
Currency.EUR = Currency(symbol="€", name="Euro", code="EUR", active=False)  # active=False

Defaults work with Annotated types too:

tag: Annotated[str, UNIQUE] = "none"

Unique-together constraints

Use UniqueTogether("group_name") to enforce that a combination of attributes is unique:

class State(DataEnum):
    __members__ = ("CA_US", "TX_US", "ON_CA", "BC_CA")

    country: Annotated[str, UniqueTogether("location")]
    code: Annotated[str, UniqueTogether("location")]
    name: str

State.CA_US = State(country="US", code="CA", name="California")
State.TX_US = State(country="US", code="TX", name="Texas")
State.ON_CA = State(country="CA", code="ON", name="Ontario")
State.BC_CA = State(country="CA", code="BC", name="British Columbia")

Individual attributes can repeat (country="US" appears twice), but the combination must be unique. Look up by passing all group attributes to get():

State.get(country="US", code="CA")  # State.CA_US
State.get(country="CA", code="ON")  # State.ON_CA

UniqueTogether works alongside UNIQUE and defaults:

class Place(DataEnum):
    __members__ = ("A", "B")

    country: Annotated[str, UniqueTogether("location")]
    code: Annotated[str, UniqueTogether("location")]
    full_name: Annotated[str, UNIQUE]
    active: bool = True

Type checker support

DataEnum uses PEP 681 (@dataclass_transform) so type checkers (mypy, pyright, ty) understand the constructor signature generated from your annotations:

Currency(symbol="$", name="US Dollar", code="USD")  # type checker knows these kwargs
Currency(symbol="$")                                  # type checker flags missing args

No-data enums

class Direction(DataEnum):
    __members__ = ("NORTH", "SOUTH", "EAST", "WEST")

Direction.NORTH = Direction()
Direction.SOUTH = Direction()
Direction.EAST = Direction()
Direction.WEST = Direction()

All members (in declaration order)

Currency.members  # (Currency.USD, Currency.EUR, Currency.GBP)

Thread safety

DataEnum is safe for use with free-threaded Python (3.14t, no-GIL). All runtime operations — get(), filter(), attribute access, iteration — are read-only on frozen data structures. Define your enum members at module load time (the normal usage pattern), and they are safe to access concurrently from any number of threads.

Safety

Members are immutable:

Currency.USD.name = "Bitcoin"  # raises AttributeError

Duplicate unique values are rejected:

Currency.GBP = Currency(symbol="£", name="Pound", code="EUR")  # raises ConfigurationError

All declared members must be assigned before use:

class Color(DataEnum):
    __members__ = ("RED", "BLUE")
    name: str

Color.RED = Color(name="Red")
Color.get("RED")  # raises ConfigurationError (BLUE not yet assigned)

Development

Set up the development environment:

make setup
source .venv/bin/activate

Run the full pipeline:

make
Task Command
Full pipeline make (sync, configure, format, lint, check, test)
Format make format
Lint make lint (format first!)
Type check make check
Test make test

License

Licensed under the Apache License, Version 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

data_enum-3.0.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

data_enum-3.0.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file data_enum-3.0.1.tar.gz.

File metadata

  • Download URL: data_enum-3.0.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for data_enum-3.0.1.tar.gz
Algorithm Hash digest
SHA256 7933241ffee842c48eeaca7f04c4760290a910d39fbc8e6ee903f13f94b0c4e9
MD5 dcbb5b933c336f2e1034f1827fdfae18
BLAKE2b-256 6003f08db10184860db285a9a9d6668601e4524e7c7131b4ac0b208f3b6a22f5

See more details on using hashes here.

File details

Details for the file data_enum-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: data_enum-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for data_enum-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3e0032086381f5d5944e152ce5881af6847a77844991b86bb6042f471ee742e
MD5 561ac2fbfa5498767bd8d1466b57fee4
BLAKE2b-256 24fcb486cb6b1a383d729141620f41cc7d94bbaf0faf79142bbd9321071d23c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page