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.
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
📊 Performance
Contract validation adds minimal overhead:
- Precondition: ~7x baseline function call
- Postcondition: ~8x baseline function call
- Memory: Constant memory usage, no leaks
See performance benchmarks for detailed analysis.
🤝 Contributing
We welcome contributions! Please see our Contributing Guide for details.
📝 Changelog
See CHANGELOG.md for a list of changes in each version.
📄 License
This project is licensed under the GPL-3.0-or-later License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ensures-0.3.0.tar.gz.
File metadata
- Download URL: ensures-0.3.0.tar.gz
- Upload date:
- Size: 28.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57620b48bc2a6e91d1ac766a82bb1df89cfa82830b7a6442845aa6f19737dd37
|
|
| MD5 |
27d917806fc1d98e621943b2e992cf8a
|
|
| BLAKE2b-256 |
f28ab0a59612bc6765df516939ff80453e7ef1c4530d3e199d4ef7f1d71720c0
|
Provenance
The following attestation bundles were made for ensures-0.3.0.tar.gz:
Publisher:
release.yml on brunodantas/ensures
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ensures-0.3.0.tar.gz -
Subject digest:
57620b48bc2a6e91d1ac766a82bb1df89cfa82830b7a6442845aa6f19737dd37 - Sigstore transparency entry: 491347467
- Sigstore integration time:
-
Permalink:
brunodantas/ensures@a1b58632ea044607cbca873f314163227cec9000 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/brunodantas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a1b58632ea044607cbca873f314163227cec9000 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ensures-0.3.0-py3-none-any.whl.
File metadata
- Download URL: ensures-0.3.0-py3-none-any.whl
- Upload date:
- Size: 29.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
510c32145fe2ee7f25bac8d7c9c6e2123c24c381dacf84c2aed9144b8ac069ca
|
|
| MD5 |
07a3535f61b5152231dd06d0d6707afd
|
|
| BLAKE2b-256 |
73c63c3b7edf647732849de54d1dc2629a80423c2441f7451a9773594f286a87
|
Provenance
The following attestation bundles were made for ensures-0.3.0-py3-none-any.whl:
Publisher:
release.yml on brunodantas/ensures
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ensures-0.3.0-py3-none-any.whl -
Subject digest:
510c32145fe2ee7f25bac8d7c9c6e2123c24c381dacf84c2aed9144b8ac069ca - Sigstore transparency entry: 491347487
- Sigstore integration time:
-
Permalink:
brunodantas/ensures@a1b58632ea044607cbca873f314163227cec9000 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/brunodantas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a1b58632ea044607cbca873f314163227cec9000 -
Trigger Event:
push
-
Statement type: