
Product
Unify Your Security Stack with Socket Basics
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
.. image:: https://travis-ci.org/radix/sumtypes.svg?branch=master :target: https://travis-ci.org/radix/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.
.. _Tagged Unions
: http://en.wikipedia.org/wiki/Tagged_union
Documentation is at https://sumtypes.readthedocs.org/
This module uses the attrs
_ library to provide features like attribute
validation and defaults.
.. _attrs
: http://pypi.python.org/pypi/attrs
Decorate your classes to make them a sum type:
.. code:: python
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
)
.. _attrs package
: https://pypi.python.org/pypi/attrs
.. _attr.ib documentation
: http://attrs.readthedocs.org/en/stable/api.html#attr.ib
Then construct them by calling the constructors:
.. code:: python
v = MyType.MyConstructor(1)
v2 = MyType.AnotherConstructor('foo', 2)
You can get the values from the tagged objects:
.. code:: python
assert v.x == 1
assert v2.x == 'foo'
assert v2.y == 2
You check the constructor used:
.. code:: python
assert type(v) is MyType.MyConstructor
And, like Scala case classes, the constructor type is a subclass of the main type:
.. code:: python
assert isinstance(v, MyType)
And the tagged objects support equality:
.. code:: python
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:
.. code:: python
@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
.
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-effectspyrsistent
_ - persistent (optimized immutable) data structures in Pythontoolz
_ - a general library of pure FP functionsfn.py
_ - a Scala-inspired set of tools, including a weird lambda syntax, option type, and monads.. _effect
: https://pypi.python.org/pypi/effect/
.. _pyrsistent
: https://pypi.python.org/pypi/pyrsistent/
.. _toolz
: https://pypi.python.org/pypi/toolz
.. _fn.py
: https://pypi.python.org/pypi/fn
FAQs
Algebraic types for Python (notably providing Sum Types, aka Tagged Unions)
We found that sumtypes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
Product
Socket is launching experimental protection for the Hugging Face ecosystem, scanning for malware and malicious payload injections inside model files to prevent silent AI supply chain attacks.
Research
/Security News
The Socket Threat Research Team uncovered a coordinated campaign that floods the Chrome Web Store with 131 rebranded clones of a WhatsApp Web automation extension to spam Brazilian users.