Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Algebraic types for Python (notably providing Sum Types, aka Tagged Unions)

Project Description

sumtypes

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, 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

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

Thanks

Thanks to Rackspace for allowing me to work on this project, and having an excellent open source employee contribution policy

License

sumtypes is licensed under the MIT license:

Copyright (C) 2015 Christopher Armstrong

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release History

Release History

This version
History Node

0.1a4

History Node

0.1a3

History Node

0.1a2

History Node

0.1a1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
sumtypes-0.1a4-py2.py3-none-any.whl (8.4 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Jun 4, 2015
sumtypes-0.1a4.tar.gz (5.1 kB) Copy SHA256 Checksum SHA256 Source Jun 4, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting