Skip to main content

Sum Types for Python

Project description

Py Sum Types

Build Status

Simple syntax to declare, match, compare, and unwrap sum types for Python

Sum Types, aka Discriminated Unions are a kind of Algebraic Data Type (ADT) found in strongly typed functional languages (among other places).

type Success = {Data: string}
type Failure = {ErrorCode: int; ErrorMessage: string}
type Response =
    | Succeeded of Success
    | Failed of Failure

let example(a: int) =
    if (a > 0) then
        Response.Succeeded <| {Data="Data!"}
    else
        Response.Failed <| { ErrorCode=401; ErrorMessage="Nope"}

let response = example(22)

match response with
    | Response.Succeeded s -> printf "%s\n" s.Data
    | Response.Failed f -> printf "%s\n" f.ErrorMessage

Definition & Useage

So how might we do this in Python?

Definition

@dataclass
class Success:
    data: str
    count: int

@dataclass
class Failure:
    error_code: int
    error_message: str

@sumtype
class Response:
    success: Success
    failure: Failure

Useage

def example(a:int) -> Response:
    if a > 0:
        return Response(Failure(error_code=403, error_message='Not Today'))
    return Response(Success('Wow such data', count=500))

response = example(22)

if response.match(Success):
    print('Look at this data {}'.format(response.data))
    actual_instance = response.unwrap()
    actual_instance.count += 1000
else:
    print('Oh what have we done sweet corgi buns!!! Error: {}'.format(response.error_message))
    if response == Failure(403, 'Not Today'):
        print('Not today!!!')

Exceptions

x = example(22)
print('Look at this data {}'.format(x.data))
# raises a SumTypeAttributeNotFound exception
@dataclass
class Failcess:
    error_message: str
    error_code: int

@sumtype
class TooManyTypes:
    success: Success
    hurray: Success
    fail: Failure
    oops: Failure

def example(a) -> TooManyTypes:
    return TooManyTypes(Success('Wow such data', 500))
    # raises a SumTypeInitError exception
    # Sum Types should have unique types

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

PySumTypes-0.0.1-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page