Skip to main content

Strictly match all the possibilities of an enum

Project description

enumatch

PyPI version codecov

Strictly 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.

TIP: Create the matcher at compile-time to have zero runtime cost.

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
with pytest.raises(ValueError, match="missing possibilities: Side.right"):
    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.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

enumatch-0.1.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: enumatch-0.1.2.tar.gz
  • Upload date:
  • Size: 2.8 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.2.tar.gz
Algorithm Hash digest
SHA256 1023f27d57b4ddd14ee6278a4274f8fe455b9d9ab81ae90cb8923aa16e388206
MD5 0f5f8ef02377dc09c129c8bb4364271d
BLAKE2b-256 3a36fd672b9ea974fef3a4ba69579f0e45084688bda6a471d10f562158228004

See more details on using hashes here.

File details

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

File metadata

  • Download URL: enumatch-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ded0bcc5d7a8c3ce55895bb600c75174fd4bd6ae9f625bf5bf3ac58ec5895fc6
MD5 645e706744733d6c8bc94b8f10d7c5de
BLAKE2b-256 cb57eb7251b8add597ea2c12c9534b5b5a2fc83e9de01f5c861e94c6dba00d02

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