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 elementSorted- Elements are in sorted orderUnique- No duplicate elementsSortedNonEmpty- Both sorted and non-empty
Numbers
Positive- Greater than zeroNonNegative- Greater than or equal to zeroNegative- Less than zeroInRange(lo, hi)- Value in [lo, hi]Percentage- Value in [0, 100]
Strings
NonBlank- Non-empty and not just whitespace
List Elements
AllPositive- All elements > 0AllNonNegative- All elements >= 0NoNone- 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 (reason required)
Skip Property Test Usage
The @skip_property_test decorator requires a reason explaining why the function cannot be property-tested. Guard warns if used without justification.
# ✅ Good - with reason
@skip_property_test("no_params: Zero-parameter function, no inputs to vary")
def get_version() -> str:
return "1.0.0"
# ❌ Bad - Guard warns about missing reason
@skip_property_test
def my_func(): ...
Valid reason categories:
no_params:- Function has no parameters to teststrategy_factory:- Returns Hypothesis strategy, not testable dataexternal_io:- Requires database/network/filesystemnon_deterministic:- Output depends on time/random state
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
Apache-2.0
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 invar_runtime-1.3.0.tar.gz.
File metadata
- Download URL: invar_runtime-1.3.0.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9a770174b087749005ba1177f4ef967603b6dfd5f19e3dce17dc3642939c708
|
|
| MD5 |
a888253fc040c07d231ee456d3378078
|
|
| BLAKE2b-256 |
58d7f0a39b4a3d1276bf93eb9cb9e25e993a14bc8683a513b3250538daf3917a
|
Provenance
The following attestation bundles were made for invar_runtime-1.3.0.tar.gz:
Publisher:
publish.yml on Tefx/Invar
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
invar_runtime-1.3.0.tar.gz -
Subject digest:
b9a770174b087749005ba1177f4ef967603b6dfd5f19e3dce17dc3642939c708 - Sigstore transparency entry: 780379064
- Sigstore integration time:
-
Permalink:
Tefx/Invar@b71c4b3414d0ba64984fbcb843442cdcae2b8b24 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/Tefx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b71c4b3414d0ba64984fbcb843442cdcae2b8b24 -
Trigger Event:
release
-
Statement type:
File details
Details for the file invar_runtime-1.3.0-py3-none-any.whl.
File metadata
- Download URL: invar_runtime-1.3.0-py3-none-any.whl
- Upload date:
- Size: 14.9 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 |
a0a7733e6ba0d221461a2dd761f9afc65e1dddd07f92f0c943fe9cde764d0147
|
|
| MD5 |
8eb751d7df231e60a513dd82fcbe17cb
|
|
| BLAKE2b-256 |
7c655d65258ce16eb37c7c27f415bf910950cf048f8016970fae96ec01f64e31
|
Provenance
The following attestation bundles were made for invar_runtime-1.3.0-py3-none-any.whl:
Publisher:
publish.yml on Tefx/Invar
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
invar_runtime-1.3.0-py3-none-any.whl -
Subject digest:
a0a7733e6ba0d221461a2dd761f9afc65e1dddd07f92f0c943fe9cde764d0147 - Sigstore transparency entry: 780379066
- Sigstore integration time:
-
Permalink:
Tefx/Invar@b71c4b3414d0ba64984fbcb843442cdcae2b8b24 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/Tefx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b71c4b3414d0ba64984fbcb843442cdcae2b8b24 -
Trigger Event:
release
-
Statement type: