Skip to main content

Design by Contract with Functional Programming

Project description

ensures

CI Python 3.10+ Code style: ruff Type checked: mypy License: GPL v3

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.

Installation

pip install ensures

Usage

precondition / require

Runs a list of functions on all args.

Returns Error if any of them fails.

from ensures import precondition


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.

from ensures import ensure


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.

from ensures import invariant


@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.

from ensures import Error, Success


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.2.0.tar.gz (20.6 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.2.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ensures-0.2.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ensures-0.2.0.tar.gz
Algorithm Hash digest
SHA256 910c3371934b88a0e1ba72b7a6444ea45cafcdf6bfeb123103c477b4a70c0354
MD5 e8a20a886b3fbb49de3cca997c7173d4
BLAKE2b-256 d6ebe0c43c7edacc0fd79c875e4814f7f27a684db5030c6ed66236c56e2454b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for ensures-0.2.0.tar.gz:

Publisher: release.yml on brunodantas/ensures

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: ensures-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ensures-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bee08d1eb14d292cdfb4aaab78776ce6fc32cee7ae601ae1898c1ca3fdcbc267
MD5 43ab8bbfb797d1aa8d411ee97375df52
BLAKE2b-256 e85cbdfc9172202e79130590abfd1227daf7291fceef98c35961d8399899933c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ensures-0.2.0-py3-none-any.whl:

Publisher: release.yml on brunodantas/ensures

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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