Skip to main content

Force match all possibilities of an enum

Project description

enumatch

PyPI version codecov

Match all the possibilities of an enum

Use case

This little match function makes matching Python's enum fields safer by forcing us to match all the possibilities either explicitely or by using a default value.

Use ... (ellipsis) for default.

Example

from enum import Enum, auto
from enumatch import match

class Side(Enum):
    left = auto()
    right = auto()

# Define a simple matcher
matcher1 = match({Side.left: "Go left", Side.right: "Go right"})

assert matcher1[Side.left] == "Go left"
assert matcher1[Side.right] == "Go right"

# Define a matcher with a default case
matcher2 = match({Side.left: "Go left", ...: "Go right"})
assert matcher2[Side.left] == "Go left"
assert matcher2[Side.right] == "Go right"

# If all the possibilities are not handled, we get error
import pytest
with pytest.raises(ValueError, match="missing possibility"):
    match({Side.left: "Go left"})

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

enumatch-0.1.1.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

enumatch-0.1.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file enumatch-0.1.1.tar.gz.

File metadata

  • Download URL: enumatch-0.1.1.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Darwin/19.6.0

File hashes

Hashes for enumatch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 db608b4daeb577ed5dc6cdd734fcdf80ecff21b08f7ce2d4d028d5ee14fdebb6
MD5 40bf0ba0a00e4d00b2941180f1280ab7
BLAKE2b-256 e80576ea61ae1ef89ebaba96428cd1789b9da57b30af6f296f2c48278d7ed0a1

See more details on using hashes here.

File details

Details for the file enumatch-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: enumatch-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Darwin/19.6.0

File hashes

Hashes for enumatch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea7249cc44958ab1f51b53ce90c0a7d9082a850f53adb84b9c947df9726161e4
MD5 b29e1846f18fd1d300ccac8820d318b3
BLAKE2b-256 8709e2c4f623118a34b0de99c22fb4308747f218c7feeead31a643e2679d02ab

See more details on using hashes here.

Supported by

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