Skip to main content

Design by Contract with Functional Programming

Project description

ensures

ensures is a simple Python package that implements the idea of Design by Contract described in the Pragmatic Paranoia chapter of The Pragmatic Programmer. That's the chapter where they say you should trust nobody, not even yourself.

Main Features

  • Verification of lists of pre/post condition and invariant functions.
  • Usage of arbitrary functions for such verification.
  • Result-type return values.

Usage

precondition / require

Runs a list of functions on all args.

Returns Error if any of them fails.

def is_positive(x):
    """Check if a number is positive."""
    return x > 0


@precondition(is_positive)
def square_root(x):
    """Calculate square root with precondition that x must be positive."""
    return x**0.5

postcondition / ensure

Runs a list of functions on the result.

Returns Error if any of them fails.

def result_is_even(result):
    """Check if result is even."""
    return result % 2 == 0


@ensure(result_is_even)  # Using the alias
def double_number(x):
    """Double a number with postcondition that result is even."""
    return x * 2

invariant

Runs a list of functions on all args.

Returns Error if any of them fails.

@invariant(lambda x: x >= 0)  # Simple lambda invariant
def increment_counter(x):
    """Increment a counter with invariant that it stays non-negative."""
    return x + 1

Result Handling

Pattern matching is supported to unpack the Return value.

result1 = square_root(1)
result2 = square_root(-1)  # This will return an Error instance

def handle_result(res):
    match res:
        case Success(value):
            print(f"Square root calculated: {value}")
        case Error(func, args):
            print(f"Precondition failed in {func.__name__} with args {args}")

handle_result(result1)
handle_result(result2)

More examples

Check examples.py

Project details


Download files

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

Source Distribution

ensures-0.1.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ensures-0.1.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file ensures-0.1.0.tar.gz.

File metadata

  • Download URL: ensures-0.1.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for ensures-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8493f35b5fde9683044eaae155be1cca792c1c8275ec964cc8df587e4a6d4eec
MD5 f6c7f1249fe6d1dba2c1e9e56c24e4d1
BLAKE2b-256 f10ded316ad579411f53a11fe1100983362daaee2f441436ab71edeb6ede8ace

See more details on using hashes here.

File details

Details for the file ensures-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ensures-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for ensures-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11ab3701cc19762c0100a32ce1b272173c947de0215644eec1ca617097b2b606
MD5 bd3db0d25656bba7ae73923a1a94ed85
BLAKE2b-256 083c56da52d8a64d1371c3a5c6cc8138836252ecbf06a5cb8ba5964b3852a9a7

See more details on using hashes here.

Supported by

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