Checkable contracts for python
Project description
PyPactum
PyPactum is a Python library designed to help both library implementers and their users to write correct code. It provides decorators for adding contract annotations, serving multiple purposes:
- Documentation
Contracts provide clear and precise specification using Python expressions instead of comments. - Runtime Verification
Contracts enable runtime verification, helping catch bugs early in the development process. - Static Analysis
Contracts can also be used for static analysis, although this would require additional implementation effort from third-party tools, which is unlikely to happen.
Usage Example
@pre(lambda ip: is_valid_ip(ip))
@pre(lambda port: is_valid_port(port))
@post(lambda result: is_valid_http_status(result))
def get_from_network(ip: str, port: int) -> int:
"""Opens a network connection and returns the HTTP status code"""
return connect_impl(ip, port) # does the actual work
Overview
Contract assertions are a mechanism to specify and potentially enforce conditions that must hold at certain points of the program.
The four key concepts are:
- Contract Kind
PyPactum knows preconditions, postconditions, and invariants. - Predicate
Predicates encapsulate the condition that must be true for an assertion to hold. - Argument Bindings
Binding variables such as arguments or the function result value makes them accessible to the predicate. - Labels
Labels can be attached to contract assertions to influence the effective evaluation semantic for that assertion.
For example, for preconditions:
block-beta
columns 5
kind_desc["Kind"]
pred_desc["Predicate"]
binding_desc["Argument Bindings"]:2
labels_desc["Labels"]
space:5
kind["@pre("]
pred["lambda x, y: cond(x, y),"]
captures["capture={'x', ...},"]
clones["clone={'y', ...},"]
labels["labels=[...])"]
kind_desc --> kind
pred_desc --> pred
binding_desc --> captures
binding_desc --> clones
labels_desc --> labels
style kind stroke-width: 0px, fill-opacity: 0
style pred stroke-width: 0px, fill-opacity: 0
style captures stroke-width: 0px, fill-opacity: 0
style clones stroke-width: 0px, fill-opacity: 0
style labels stroke-width: 0px, fill-opacity: 0
References
PEP-0316 Programming by Contract for Python
This PEP wants to add contracts to the language. It has been around since 2003. I don't know what
its status is.
P2900 Contracts for C++
A proposal to add contracts to C++. I took some inspiration from there.
Project details
Release history Release notifications | RSS feed
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 pypactum-0.3.4.tar.gz.
File metadata
- Download URL: pypactum-0.3.4.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d17098a6fadf6d161857f2f53d0b2e27d9687436acc122e8cb24755c2c668a17
|
|
| MD5 |
8e4e268ff22e26730a05c1978ee297ce
|
|
| BLAKE2b-256 |
f4014d94b8e6b17b8ea683e715fca3882010990b3458a57d81c4cc331cf54164
|
Provenance
The following attestation bundles were made for pypactum-0.3.4.tar.gz:
Publisher:
python-package.yml on jan-moeller/pactum
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypactum-0.3.4.tar.gz -
Subject digest:
d17098a6fadf6d161857f2f53d0b2e27d9687436acc122e8cb24755c2c668a17 - Sigstore transparency entry: 179448196
- Sigstore integration time:
-
Permalink:
jan-moeller/pactum@956eb649d3ab4170ca06b538bbd94805df54b567 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jan-moeller
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@956eb649d3ab4170ca06b538bbd94805df54b567 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pypactum-0.3.4-py3-none-any.whl.
File metadata
- Download URL: pypactum-0.3.4-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4348c14ef20aaf657c0b21068e41c9868cf69e1a2d118932106c65df8f37b58
|
|
| MD5 |
69dcf02d68e8e63708158d74bd27068b
|
|
| BLAKE2b-256 |
8093ce4c5a77aa266a5b61a0489521ef8d596bc8f5ceff6f024abda5738c6016
|
Provenance
The following attestation bundles were made for pypactum-0.3.4-py3-none-any.whl:
Publisher:
python-package.yml on jan-moeller/pactum
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypactum-0.3.4-py3-none-any.whl -
Subject digest:
d4348c14ef20aaf657c0b21068e41c9868cf69e1a2d118932106c65df8f37b58 - Sigstore transparency entry: 179448197
- Sigstore integration time:
-
Permalink:
jan-moeller/pactum@956eb649d3ab4170ca06b538bbd94805df54b567 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jan-moeller
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@956eb649d3ab4170ca06b538bbd94805df54b567 -
Trigger Event:
push
-
Statement type: