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 details)

Uploaded Python 3

File details

Details for the file PySumTypes-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: PySumTypes-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for PySumTypes-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e71dfbaa185afaec3baf1d54232ee120c218c29391255775051ee8922fcdb1c7
MD5 c974a3f22a0af60e9e01bb0461fd2c59
BLAKE2b-256 1ba92997e4e64c6a0b5e87068497c797a0f543c32051ab0404e92e291800b908

See more details on using hashes here.

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