Skip to main content

EnumHandler

If you're like me, you were disappointed to see that structural pattern matching (PEP 622) landed in Python without any of the proposed exhaustiveness guarantees (PEP 634). Although match and case are still useful, exhaustive matching over an enum is a comfortable idiom in other languages, and I was looking forward to first-class treatment of this in Python. Worse, I ended up writing a number of repetitive exhaustiveness tests:

import pytest

from myproject import Colors, name_of_color

@pytest.parametrize(MyEnum)
def test_name_of_color_exhaustiveness(value):
    assert name_of_color(value)

Tests that don't care about the return value of the function under test are a hallmark of type system failures.

This library fixes that by providing a base class for classes that exhaustively cover an enumeration. Upon class definition, EnumHandler will validate the exhaustiveness and uniqueness of handlers registered on any subclass using the EnumHandler.register decorator.

from enum import Enum, auto

from enumhandler import EnumHandler, handles


class Colors(Enum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()


class NameOfColor(EnumHandler, enum=Colors):
    @handles(Colors.RED)
    def red(self) -> str:
        return "Red"

    @handles(Colors.GREEN)
    def green(self) -> str:
        return "Green"

    @handles(Colors.BLUE)
    def blue(self) -> str:
        return "Blue"

The class is instantiated with a member of the enum, and the resulting instance is callable:

>>> print(NameOfColor(Colors.RED)())
Red

An InvalidEnumHandler exception is raised in three cases:

  • Not all enum values are handled.
  • One enum value has multiple registered handlers.
  • A handler is registered for the incorrect enum.
  • A handler is registered on an existing class.

Please note that subclassing is currently not supported.

Metadata

Release files for enumhandler 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for enumhandler 0.2.0
File Size Uploaded
enumhandler-0.2.0.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for enumhandler 0.2.0
File Interpreter ABI Platform
enumhandler-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / enumhandler-0.2.0.tar.gz

Download URL enumhandler-0.2.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a37c61e64763c631d6b3af3fcd5b6a6e54a8b53e5e3cb2743d0d864f5e63e9d7
BLAKE2b-256 checksum
How to use checksums
ceba50d93e6f14777cc4782e63cf565f9512da79faa98c39647c11cbdcc6924e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / enumhandler-0.2.0-py3-none-any.whl

Download URL enumhandler-0.2.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60c135dc29871972273394f34a3ee6ded76768af5675cedf26323f8bdf0cfd99
BLAKE2b-256 checksum
How to use checksums
996222924efc16966c0a6c36f535c337b1f2637ea210aec4b10e0f0a0aea442a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page