Skip to main content

A powerful, expressive and lightweight design-by-contract framework

Project description

ContractMe

coverage Checked with pyright Code style: black

A lightweight and adaptable framework for design-by-contract in python

Example code

Here are some examples:

result

@precondition(lambda x: x >= 0)
@postcondition(lambda x, result: eps_eq(result * result, x))
def square_root(x: float) -> float:
    return x**0.5

old

@precondition(lambda l, n: n >= 0 and round(n) == n)
@postcondition(lambda l, n: len(l) > 0)
@postcondition(lambda l, n: l[-1] == n)
@postcondition(lambda l, n, old: l[:-1] == old.l)
def append_count(l: list[int], n: int):
    l.append(n)

Using annotations

@annotated
def incr(v : int) -> int:
    return v + 1

Supports annotations and PEP-593 using the annotated-types library. Note: annodated_types.MultipleOf follows the Python semantics. Note 2: Following an open-world reasoning, any unknown annotation is considered to be correct, so it won't cause a check failure.

from typing import TypeAlias, Annotated
from annotated_types import MultipleOf

Even: TypeAlias = Annotated[int, MultipleOf(2)]

@annotated
def square(v : Even) -> Even
    return v * v

Test

uv run pytest

Changelog

  • v1.2.0

Full support of annotated-types library for checking PEP-593 compatible type annotations automatically through the @annotated decorator.

Generated contracted functions are now of a ContractedFunction class, with a original_call attribute that contains the function without contracts checking.

Pyright check for the totality of the code.

  • v1.1.0

Contracts can be disabled at runtime with ignore_preconditions() and ignore_postconditions()

Contracts are disabled from the start with python optimized (-O) flag.

Fix a bug where contracts would hide an incorrect function call

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

contractme-1.2.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

contractme-1.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file contractme-1.2.0.tar.gz.

File metadata

  • Download URL: contractme-1.2.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.2

File hashes

Hashes for contractme-1.2.0.tar.gz
Algorithm Hash digest
SHA256 0d5af6c3ee6140f324c52d46303bc7fc94e8db775d0147185e28c5206d905ce5
MD5 111444601d1e3cb98b31369f0cf6844f
BLAKE2b-256 d5a2fdd5fcb3a47fa5ab1ee9d73dafa25eace150c85728a37abbacf280c7bcf9

See more details on using hashes here.

File details

Details for the file contractme-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: contractme-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.2

File hashes

Hashes for contractme-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a022918df6f827df04f81db31bc7c31f639bc50fbaf29dfd6781927d36c3930f
MD5 0aaa7e36baa545e3d4f0a4496bacfd40
BLAKE2b-256 2aaeea11c91e3cc380b2e1d6b784dde9d64c50a457a06ec8252d39de9c3b8b78

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