sumtypes
raw JSON → 0.1a6 verified Mon Apr 27 auth: no python deprecated
Provides algebraic data types (sum types / tagged unions) for Python, with pattern matching support, inspired by Rust and Haskell. Current version is 0.1a6 (alpha); no release cadence established.
pip install sumtypes Common errors
error NameError: name 'Case' is not defined ↓
cause Forgot to import Case from sumtypes.
fix
Add
from sumtypes import Case to your imports. error TypeError: Can't instantiate abstract class Status without an implementation ↓
cause Trying to call Status() directly instead of a variant (e.g., Status.Active()).
fix
Use
Status.Active() to create an instance. error AttributeError: 'Status' object has no attribute 'value' ↓
cause Accessing .value on a variant that doesn't carry data (e.g., an empty Case).
fix
Only access .value on variants with parameters; use isinstance to determine variant first.
Warnings
deprecated Library is in alpha (0.1a6) and appears unmaintained; last updated in 2015. Consider alternatives like 'union' or 'adt' for production use. ↓
fix Migrate to 'adt' or 'union' library for actively maintained sum types.
gotcha Avoid Python 3.10+ pattern matching (match/case) as it conflicts with sumtypes.Case naming. Use isinstance instead. ↓
fix Always check variant type via isinstance, not structural pattern matching.
gotcha sumtypes.Enum is not iterable and cannot be used like stdlib Enum. Do not call list() on it. ↓
fix If you need iteration, maintain a separate list of variants.
Imports
- SumType wrong
import sumtypes.SumTypecorrectfrom sumtypes import SumType - Enum wrong
from enum import Enumcorrectfrom sumtypes import Enum - Case
from sumtypes import Case
Quickstart
from sumtypes import SumType, Case, Enum
class Status(SumType):
Active = Case()
Inactive = Case()
Pending = Case(str)
statuses = [Status.Active(), Status.Inactive(), Status.Pending("waiting")]
for s in statuses:
if isinstance(s, Status.Active):
print("active")
elif isinstance(s, Status.Inactive):
print("inactive")
else:
print(f"pending: {s.value}")