Skip to main content

Strictly match all the possibilities of an enum

Project description

enumatch

PyPI version PyPI pyversions Test Coverage

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.4.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

enumatch-0.1.4-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: enumatch-0.1.4.tar.gz
  • Upload date:
  • Size: 3.0 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.4.tar.gz
Algorithm Hash digest
SHA256 ad8d7a275825a532270a0603def650a05dc51ccbfe3cf96067e35d450aafcf77
MD5 94facdf4a67ccd581f443808dd2eb2a6
BLAKE2b-256 762d955dec816d7f9fd83c9aa7304869bbe3b8c9a083c0f37a0bfc2e4abf634f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: enumatch-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 3.2 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6a8501a57f0ef48b966e31dcb8c7bfb52718bfb1be0bcc0019fd721040f18431
MD5 9904f750d1ff1bf3ab5f09a232a0640a
BLAKE2b-256 f4dac71b7fa621fdaec3b79155151dd9013dabdc57f8a266fcae3471032e862f

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