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
Metadata
Release files for invar-runtime 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| invar_runtime-1.3.0.tar.gz | 12.5 kB | With provenance | Details for invar_runtime-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| invar_runtime-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for invar_runtime-1.3.0-py3-none-any.whl |
Total release size: 27.4 kB
Release files / invar_runtime-1.3.0.tar.gz
| Download URL | invar_runtime-1.3.0.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b9a770174b087749005ba1177f4ef967603b6dfd5f19e3dce17dc3642939c708
|
|
BLAKE2b-256 checksum How to use checksums |
58d7f0a39b4a3d1276bf93eb9cb9e25e993a14bc8683a513b3250538daf3917a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.
Transparency logRelease files / invar_runtime-1.3.0-py3-none-any.whl
| Download URL | invar_runtime-1.3.0-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a0a7733e6ba0d221461a2dd761f9afc65e1dddd07f92f0c943fe9cde764d0147
|
|
BLAKE2b-256 checksum How to use checksums |
7c655d65258ce16eb37c7c27f415bf910950cf048f8016970fae96ec01f64e31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.
Transparency logSecurity for invar-runtime 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for invar_runtime-1.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.