Skip to main content

Checkable contracts for python

Project description

PyPactum

Python package

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:

  1. Documentation
    Contracts provide clear and precise specification using Python expressions instead of comments.
  2. Runtime Verification
    Contracts enable runtime verification, helping catch bugs early in the development process.
  3. 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:

  1. Contract Kind
    PyPactum knows preconditions, postconditions, and invariants.
  2. Predicate
    Predicates encapsulate the condition that must be true for an assertion to hold.
  3. Argument Bindings
    Binding variables such as arguments or the function result value makes them accessible to the predicate.
  4. 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


Download files

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

Source Distribution

pypactum-0.3.3.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

pypactum-0.3.3-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file pypactum-0.3.3.tar.gz.

File metadata

  • Download URL: pypactum-0.3.3.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

Hashes for pypactum-0.3.3.tar.gz
Algorithm Hash digest
SHA256 1594bf01426fc4d9a1504996f4bc7192c5a7f81206461483cfe143b1acb93351
MD5 0878fe7c8452dfb7c9b1d529779abf73
BLAKE2b-256 dee28337ad58b1a3950e4285c220006257e2b5bb8c985cf4cdaead32a77b4994

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypactum-0.3.3.tar.gz:

Publisher: python-package.yml on jan-moeller/pactum

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

File details

Details for the file pypactum-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: pypactum-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pypactum-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 573a1e8ec513290dfca7858a74b041ba1ebd26dc17ba716205daac4001df4661
MD5 c7b9782927e3dc2026af8e9dc22b55e0
BLAKE2b-256 66423ad87ccbd8213926bbe2b3f866141be458d1f03e434704dbe6afe7fde9d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypactum-0.3.3-py3-none-any.whl:

Publisher: python-package.yml on jan-moeller/pactum

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