Skip to main content

Value-generated string enums with automatic naming conventions for Python

Project description

Enum Framework

Overview

vcti-enum provides string-valued StrEnum base classes whose member values are auto-generated from a naming convention (lowercase, camelCase, PascalCase, etc.), plus an EnumCoder for flexible serialization to and from custom string representations.

The Pydantic helpers (setup_enum_for_pydantic, @enum_for_pydantic) exist to decouple the internal enum representation from the external API surface. Internally the enum can be an IntEnum, a StrEnum, or anything else; externally the model speaks stable strings that appear in JSON, schemas, and config files. Changing the internal representation never changes the wire contract.

The package has zero required dependencies. Pydantic is optional and only needed for the FIELD attribute and the @enum_for_pydantic attachment to take effect.


Installation

pip install vcti-enum

In requirements.txt

vcti-enum>=1.1.0

In pyproject.toml dependencies

dependencies = [
    "vcti-enum>=1.1.0",
]

Quick Start

Value-generated enums

from vcti.enum import EnumValueLowerCase, auto_enum_value

class FileFormat(EnumValueLowerCase):
    JSON = auto_enum_value()    # value: "json"
    CSV = auto_enum_value()     # value: "csv"
    PARQUET = auto_enum_value() # value: "parquet"

FileFormat.JSON.value  # "json"
FileFormat.JSON == "json"  # True (StrEnum)

EnumCoder for serialization

from enum import Enum
from vcti.enum import EnumCoder

class Color(Enum):
    RED = 1
    GREEN = 2

coder = EnumCoder(Color, value_generator=lambda e: e.name.lower(), default=Color.RED)

coder.encode(Color.RED)    # "red"
coder.decode("green")      # Color.GREEN
coder.default              # "red"
coder.list                 # ["red", "green"]

Pydantic integration

The mental model is three layers — internal enum, encoded external string, bridge encoder. Pydantic models only ever see the encoded strings, so the internal enum can change freely.

from pydantic import BaseModel

from vcti.enum import EnumValueLowerCase, auto_enum_value, enum_for_pydantic

@enum_for_pydantic(default="JSON")
class FileFormat(EnumValueLowerCase):
    JSON = auto_enum_value()
    CSV = auto_enum_value()

class ExportConfig(BaseModel):
    format: FileFormat.FIELD          # bundled type + default + description

ExportConfig().format                 # "json"
ExportConfig(format="csv").format     # "csv"

FileFormat.FIELD is an Annotated[Literal["json", "csv"], Field(...)] built by the decorator. It bundles the field type, the default, and a generated description. The equivalent hand-written form is:

class ExportConfig(BaseModel):
    format: Literal["json", "csv"] = Field(
        default="json",
        description="Supported values: (json/csv). Default: json",
    )

Decoupling internal representation: IntEnum example

from enum import IntEnum
from pydantic import BaseModel

from vcti.enum import setup_enum_for_pydantic

class Theme(IntEnum):
    LIGHT = 1
    DARK = 2

setup_enum_for_pydantic(
    Theme,
    value_generator=lambda e: e.name.lower(),  # "LIGHT" -> "light"
    default_member="LIGHT",
)

class UiSettings(BaseModel):
    theme: Theme.FIELD

UiSettings().theme              # "light"
UiSettings(theme="dark").theme  # "dark"
Theme.CODER.decode("light")     # Theme.LIGHT (the internal IntEnum member)

The model field is a Literal["light", "dark"]not an IntEnum. You could renumber LIGHT = 100 tomorrow and nothing on the API side notices.


Enum Base Classes

Class Convention FIRST_VALUE becomes
EnumValueSameAsName Unchanged FIRST_VALUE
EnumValueLowerCase lower_case first_value
EnumValueCamelCase camelCase firstValue
EnumValuePascalCase PascalCase FirstValue
EnumValueCapitalizedPhrase Title Case First Value
EnumValueSpaceSeparatedLower space lower first value

All classes extend StrEnum -- members are string instances.


Public API

Symbol Import Purpose
auto_enum_value() vcti.enum Triggers automatic value generation
create_enum_class() vcti.enum Factory to create custom enum base classes
EnumValueSameAsName vcti.enum Base class: value = member name
EnumValueLowerCase vcti.enum Base class: value = lowercase
EnumValueCamelCase vcti.enum Base class: value = camelCase
EnumValuePascalCase vcti.enum Base class: value = PascalCase
EnumValueCapitalizedPhrase vcti.enum Base class: value = Title Case
EnumValueSpaceSeparatedLower vcti.enum Base class: value = space separated
EnumCoder vcti.enum Flexible enum serialization/deserialization
setup_enum_for_pydantic() vcti.enum Attach encoder attributes to an enum class
enum_for_pydantic() vcti.enum Decorator form of the setup function
get_enum_field_description() vcti.enum Build a description string (kept for back-compat)

Attributes attached by setup_enum_for_pydantic / @enum_for_pydantic

Attribute Type What it contains
CODER EnumCoder The encoder instance for this enum
ENCODED_DEFAULT str Encoded string of the default member
ENCODED_LIST list[str] All encoded strings, as a list
ENCODED_TUPLE tuple[str, ...] All encoded strings, as a tuple
FIELD Annotated[Literal[...], Field(...)] Bundled type + default + description

Deprecated aliases (kept for back-compat)

Deprecated Use instead
DEFAULT_VALUE ENCODED_DEFAULT
LIST ENCODED_LIST
TUPLE ENCODED_TUPLE
LITERAL FIELD (the Literal[...] is wrapped inside)
get_enum_field_description(E) description is auto-baked into E.FIELD

Documentation

  • Design -- Concepts, factory pattern, encoder layering, architecture decisions
  • Source Guide -- File descriptions and execution flow traces
  • Extension Guide -- How to add new enum base classes
  • API Reference -- Autodoc for all modules

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

vcti_enum-1.1.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

vcti_enum-1.1.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file vcti_enum-1.1.0.tar.gz.

File metadata

  • Download URL: vcti_enum-1.1.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for vcti_enum-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c9bf2511e491945d28354bca7be435601db2aeec23ed33fa1a7a0cb4dcdf41b5
MD5 075d21ac137baaca9f5f837cadb358de
BLAKE2b-256 58874c9d923f5b73cf428ed5cc799ea967c1033d6391920f381f977e4ed1b370

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_enum-1.1.0.tar.gz:

Publisher: release.yml on vcollab/vcti-python-enum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vcti_enum-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: vcti_enum-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for vcti_enum-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 441b538a38a4ebad4235e67a20319d1dc6dce0f02335e44752e6ff0882f14a23
MD5 c9bbb7a9867cf55d1d89f8685ea440b5
BLAKE2b-256 b3e7ba472ffce8e252add1201eb56615af86f070152e917a5e19d6db13f00f01

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_enum-1.1.0-py3-none-any.whl:

Publisher: release.yml on vcollab/vcti-python-enum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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