Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Python Choice Types

choicetypes brings powerful and simple algebraic sum types to Python.

Usage

New Choice types are constructed using sytax similar to standard library enums and dataclasses:

from choicetypes import Choice


class IpAddr(Choice):
    V4: tuple[int, int, int, int]
    V6: str


home = IpAddr(V4=(127, 0, 0, 1))
loopback = IpAddr(V6="::1")

A choice consists of mutually exclusive variants. In this example, any instance of IpAddr must be either a V4 or V6 address. Variants are just normal attributes and can be accessed as such. But they're also designed to work seamlessly with structural pattern matching, introduced in Python 3.10:

for ip in (home, loopback):
    match ip:
        case IpAddr(V4=fields):
            print("{}:{}:{}:{}".format(*fields))
        case IpAddr(V6=text):
            print(text)

For a complete overview of what Choice type can do, see the official documentation.

Installation

The choicetypes package is available on PyPi:

pip install choicetypes

It is written in pure Python with zero dependencies and tested against Python 3.7 and newer.

Background

Algebraic choice types have various other names (sum types, tagged unions, etc.) and exist in a number of programming languages. The primary inspiration for choicetypes was Rust's Enum type.

Python's Enum allows you to express mutually exclusive variants, but not associated data. Its dataclass (and similar) types store data but cannot represent mutually exclusive variants. The core idea behind algebraic sum types is to store these two pieces of information simultaneously.

I wanted to build a type that could accomplish this in a similar style to Rust. Importantly, it had to work cleanly with structural pattern matching and type hinting.

Metadata

Release files for choicetypes 0.1.0rc1

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

Source distribution (sdist)

Source distribution for choicetypes 0.1.0rc1
File Size Uploaded Provenance
choicetypes-0.1.0rc1.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 10.0 kB

Release files / choicetypes-0.1.0rc1.tar.gz

Download URL choicetypes-0.1.0rc1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7102e1c3fd93f1ea7bd4e0c1d8dc89af9aeb05f5dadedbb565263a44e603dbb5
BLAKE2b-256 checksum
How to use checksums
1f0b12a4bcc974364be7e5a42d4e2495c9651a5980db32fa598d93c85790a71a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.10.6 Darwin/21.6.0

Release files / choicetypes-0.1.0rc1-py3-none-any.whl

Download URL choicetypes-0.1.0rc1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33201af622da8426416728b51b8e3dd10cee08a697a6a930c52f7c320e8fed4e
BLAKE2b-256 checksum
How to use checksums
7027bf3bb32653591a7fa39720b721488a015c746e33a4d539ec49892dbd4f5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.10.6 Darwin/21.6.0

Security for choicetypes 0.1.0rc1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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