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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: enumatch-0.1.3.tar.gz
  • Upload date:
  • Size: 2.9 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.3.tar.gz
Algorithm Hash digest
SHA256 aa066ad9d4a2c90fb7c31dea7aa2a91af62072114a802cd2330ffeef212f7ac5
MD5 94b16ad94419ccd80843ec8e452cc030
BLAKE2b-256 391a28c1e97e43e8f7e2d29877aae5e55de8dd8e315403e8b91350d408c9161f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: enumatch-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ea45fc844bd969853c7cd030c8a22bc348d3bceaff95b4333cf3c972500fe757
MD5 dc43bc761954073bede9c0b03f6f4ee7
BLAKE2b-256 c1c60d1492060e1ba18df17cf72f54223e64c9640a88f47ea240fd681d1981c2

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