Skip to main content

Lightweight runtime contracts for Invar projects

Project description

invar-runtime

Lightweight runtime contracts for Python projects using Invar.

Installation

pip install invar-runtime

Usage

from invar_runtime import pre, post, Contract, NonEmpty, Positive

# Use built-in contracts
@pre(NonEmpty)
def first(xs: list) -> int:
    return xs[0]

# Create custom contracts
Even = Contract(lambda x: x % 2 == 0, "even")

@pre(Positive & Even)
def half(n: int) -> int:
    return n // 2

# Compose contracts
@post(NonEmpty)
def get_items() -> list:
    return [1, 2, 3]

Available Contracts

Collections

  • NonEmpty - Collection has at least one element
  • Sorted - Elements are in sorted order
  • Unique - No duplicate elements
  • SortedNonEmpty - Both sorted and non-empty

Numbers

  • Positive - Greater than zero
  • NonNegative - Greater than or equal to zero
  • Negative - Less than zero
  • InRange(lo, hi) - Value in [lo, hi]
  • Percentage - Value in [0, 100]

Strings

  • NonBlank - Non-empty and not just whitespace

List Elements

  • AllPositive - All elements > 0
  • AllNonNegative - All elements >= 0
  • NoNone - No None values

Decorators

  • @must_use(reason) - Mark return value as must-use
  • @must_close - Mark class as requiring explicit cleanup
  • @strategy(**params) - Specify Hypothesis strategies
  • @skip_property_test(reason) - Skip property-based testing

Loop Invariants

from invar_runtime import invariant

while lo < hi:
    invariant(0 <= lo <= hi <= len(arr), "bounds check")
    mid = (lo + hi) // 2
    ...

Development Tools

For static analysis and verification tools, install invar-tools:

pip install invar-tools
# or use without installing:
uvx invar-tools guard

License

MIT

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

invar_runtime-1.0.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

invar_runtime-1.0.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file invar_runtime-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for invar_runtime-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a843c2392769a40552611069ba35ae7df676333a7c024e38a78dfe3b83e13ca
MD5 58bcbb92c1e0bec08127214a8250da93
BLAKE2b-256 55593760ca6c1017d761f50956c4f9e853392bf5972b53b2cc91541d3759b404

See more details on using hashes here.

Provenance

The following attestation bundles were made for invar_runtime-1.0.0.tar.gz:

Publisher: publish.yml on Tefx/Invar

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

File details

Details for the file invar_runtime-1.0.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for invar_runtime-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5622a34786421cc83ea75faaefedb4a573267320f5ad9248cc4d56a21b050757
MD5 27714ea9c04845b38a97c3077c5fd23b
BLAKE2b-256 8aa83161309b6a01b912561bbc153785eba554cd3b0c5f60a7ba5d4b1c55e1b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for invar_runtime-1.0.0-py3-none-any.whl:

Publisher: publish.yml on Tefx/Invar

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