Skip to main content
Pre-release

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

sumtypes

https://travis-ci.org/radix/sumtypes.svg?branch=master

sumtypes provides Algebraic Data Types for Python. The main benefit is the implementation of Sum Types (aka Tagged Unions), which Python doesn’t have any native representation for. Product Types are just objects with multiple attributes.

Documentation is at https://sumtypes.readthedocs.org/

This module uses the attrs library to provide features like attribute validation and defaults.

Example

Decorate your classes to make them a sum type:

import attr
from sumtypes import sumtype, constructor, match

@sumtype
class MyType(object):
    # constructors specify names for their arguments
    MyConstructor = constructor('x')
    AnotherConstructor = constructor('x', 'y')

    # You can also make use of any feature of the attrs
    # package by using attr.ib in constructors
    ThirdConstructor = constructor(
        one=attr.ib(default=42),
        two=attr.ib(validator=attr.validators.instance_of(int)))

(attrs package, and attr.ib documentation)

Then construct them by calling the constructors:

v = MyType.MyConstructor(1)
v2 = MyType.AnotherConstructor('foo', 2)

You can get the values from the tagged objects:

assert v.x == 1
assert v2.x == 'foo'
assert v2.y == 2

You check the constructor used:

assert type(v) is MyType.MyConstructor

And, like Scala case classes, the constructor type is a subclass of the main type:

assert isinstance(v, MyType)

And the tagged objects support equality:

assert v == MyType.MyConstructor(1)
assert v != MyType.MyConstructor(2)

Simple pattern matching is also supported. To write a function over all the cases of a sum type:

@match(MyType)
class get_number(object):
    def MyConstructor(x): return x
    def AnotherConstructor(x, y): return y
    def ThirdConstructor(one, two): return one + two

assert get_number(v) == 1
assert get_number(v2) == 2

match ensures that all cases are handled. If you really want to write a ‘partial function’ (i.e. one that doesn’t cover all cases), use match_partial.

See Also

Over the past few years, the ecosystem of libraries to help with functional programming in Python has exploded. Here are some libraries I recommend:

  • effect - a library for isolating side-effects

  • pyrsistent - persistent (optimized immutable) data structures in Python

  • toolz - a general library of pure FP functions

  • fn.py - a Scala-inspired set of tools, including a weird lambda syntax, option type, and monads

Metadata

Release files for sumtypes 0.1a6

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

Source distribution (sdist)

Source distribution for sumtypes 0.1a6
File Size Uploaded
sumtypes-0.1a6.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sumtypes 0.1a6
File Interpreter ABI Platform
sumtypes-0.1a6-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 11.1 kB

Release files / sumtypes-0.1a6.tar.gz

Download URL sumtypes-0.1a6.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1a6ff095e06a1885f340ddab803e0f38e3f9bed81f9090164ca9682e04e96b43
BLAKE2b-256 checksum
How to use checksums
3aaa1aa52ae948166291cf615687a733151d4567645beac7e51b7bae3fd88ad4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

Release files / sumtypes-0.1a6-py2.py3-none-any.whl

Download URL sumtypes-0.1a6-py2.py3-none-any.whl
Size 5.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3e9d71322dd927d25d935072f8be7daec655ea292fd392359a5bb2c1e53dfdc3
BLAKE2b-256 checksum
How to use checksums
5a294ef1ff91dad16f8396bab999a09011d4939951f47b9729b3f73cc1f74756
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5
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