skillfed

sumtypes

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

sumtypes v0.1a6 187.0K downloads/30d#9,972 on PyPI44
Permissive license MIT Abandoned released

What it is and what it does

sumtypes brings algebraic data types to Python by implementing sum types (also called tagged unions), which let you define a type with multiple named constructor variants. Each variant can carry its own set of attributes. The package builds on attrs to provide attribute validation, defaults, and other features. You decorate a class with @sumtype, define constructors as class attributes, and then instantiate them by calling those constructors; the resulting objects track which constructor was used and can be pattern-matched over all cases.

The main use case is writing code that handles multiple distinct cases of a logical type in a type-safe, exhaustive way. Instead of using strings or integers to tag variants, sum types make the structure explicit and let the match decorator verify that all cases are handled. This is particularly useful in functional programming styles or when modeling domain logic with multiple states or alternatives.

Use it for:

  • Model domain types with multiple variants such as Result as Success or Failure, or AST nodes with different expression types
  • Write exhaustive pattern-matching functions over all constructor cases without risk of missing a case
  • Define state machines or state enums where each state can carry different associated data
  • Implement functional-style error handling with explicit Success and Error constructors
  • Build parsers or interpreters that need to represent different kinds of syntax tree nodes

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides algebraic data types for Python, specifically sum types (tagged unions) that let you define and pattern-match over multiple constructor variants of a single type.

No. The package is abandoned (last commit 2021-11-30, no activity for 1718 days), and maintenance compatibility with current Python versions is unverified. For new projects, consider whether native language features or actively maintained alternatives better serve your needs.

Install

sumtypes on PyPI

pip

pip install sumtypes

uv

uv add sumtypes

poetry

poetry add sumtypes

Installing sumtypes

Before you install

Low install friction with a single runtime dependency on attrs. However, the package is abandoned as of 2021-11-30 with no commits since then, so maintenance and compatibility with modern Python versions are concerns.

License in practice

MIT license is permissive, allowing commercial and private use with minimal restrictions.

Quickstart

pip install sumtypes

from sumtypes import sumtype, constructor, match

@sumtype
class Color(object):
    Red = constructor()
    Green = constructor()
    Blue = constructor('intensity')

red = Color.Red()
blue = Color.Blue(42)

@match(Color)
class describe(object):
    def Red(): return 'red'
    def Green(): return 'green'
    def Blue(intensity): return 'blue'

Verify before relying

  • Whether the package works reliably with modern Python versions given its abandoned status since 2021-11-30
  • Whether attrs version compatibility has drifted and could cause runtime issues
  • Whether pattern matching remains the recommended approach or if newer Python versions offer better alternatives

Package facts

License MIT (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies 1 — attrs
Maintenance abandoned — 1,718 days since the last release
Last repo commit
First released
Downloads 187,040/month — #9,972 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: sumtypes-0.1a6-py2.py3-none-any.whl

License :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 2Programming Language :: Python :: 3

Tags

algebraic data types pythonsum types tagged unionspattern matching pythontagged union implementationconstructor variants pythonfunctional programming typesenum-like discriminated unions
algebraic-typespattern-matchingfunctional-programming

More Software Development packages