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.1.tar.gz (17.3 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.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ensures-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bd3785b3d834f1fa976cfd1bf45f897f330d904e2938c2043e7590cec44998d5
MD5 33f454367492fc87fcc16aa36e0d16ec
BLAKE2b-256 bcfec89b48f6d576c3d37c639f578b9bd4fa67b7eb28d3c6902ea6b2580c9ca2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ensures-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09c14d9894a755e77cba77437212a65a51b21b20333fd8daf21430486a9833cb
MD5 de92436a45d6df99c1a662c7714672dc
BLAKE2b-256 5ea68d9c57d3a99bcf8f24872de2a21ab1e97fc7b6b28ab9856fdfa6ffe85ac1

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