icontract
Provide design-by-contract with informative violation messages.
Decision gist · record as of 2026-08-14
Yes, if you want runtime contract enforcement with detailed diagnostics. The permissive MIT license, low install friction, and lack of known vulnerabilities make it safe to adopt. Aging maintenance (197 days since last release) is a minor concern but not a blocker—the library is stable and the repository remains active. Best suited for teams that value explicit contract documentation and detailed failure messages over minimal runtime overhead.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Low friction install with three lightweight runtime dependencies (asttokens, contextvars, typing_extensions).
- Maintenance status is aging—last release was 197 days ago—but the repository remains active and unarchived with 411 stars.
License · maintenance · safety
permissive license (permissive) — MIT license (permissive) places no restrictions on use, modification, or distribution in proprietary or open-source projects.
last release 2026-01-29 (197 days) · last repo commit 2026-01-29 · 411 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 408,319 downloads/mo, #6,881 on PyPI
Alternatives
Verify before relying
pip install icontract
import icontract
@icontract.require(lambda x: x > 3)
def some_func(x: int) -> None:
pass
some_func(x=5) # succeeds
some_func(x=1) # raises ViolationError with details- Performance overhead of contract checking at runtime compared to assertion-based alternatives
- Compatibility with async/await patterns and coroutines
- Whether inheritance contract weakening/strengthening works correctly with multiple inheritance
What it is and what it does
icontract is a Python library that implements design-by-contract through decorators, allowing you to declare preconditions (what must be true before a function runs), postconditions (what must be true after), and invariants (what must always be true for a class). When a contract is violated, it raises a ViolationError with a detailed message that includes the contract condition itself, the values of all relevant variables at the time of violation, and any custom description you provided.
The library is built on three runtime dependencies: asttokens, contextvars, and typing_extensions. It supports Python 3.6 through 3.13 and has been in production use since 2018. Unlike simpler assertion-based approaches, icontract automatically extracts and displays variable values without requiring you to write custom error messages, following the DRY principle. It also supports contract inheritance, allowing subclasses to weaken preconditions and strengthen postconditions.
Use it for
- Add runtime validation to library APIs to catch misuse early with informative error messages
- Document and enforce class invariants in complex stateful objects to prevent invalid states
- Verify function inputs and outputs in data processing pipelines to catch logic errors
- Generate automated test cases by inferring test strategies from contracts
- Enforce contracts on HTTP APIs to validate request and response contracts
Worth the install?
AI-flagged interpretation of the facts on this page. Verify before relying on it.
Yes, if you want runtime contract enforcement with detailed diagnostics.
The permissive MIT license, low install friction, and lack of known vulnerabilities make it safe to adopt. Aging maintenance (197 days since last release) is a minor concern but not a blocker—the library is stable and the repository remains active. Best suited for teams that value explicit contract documentation and detailed failure messages over minimal runtime overhead.
Install
icontract on PyPI
Before you install
Low friction install with three lightweight runtime dependencies (asttokens, contextvars, typing_extensions). Maintenance status is aging—last release was 197 days ago—but the repository remains active and unarchived with 411 stars.
License in practice
MIT license (permissive) places no restrictions on use, modification, or distribution in proprietary or open-source projects.
Quickstart
pip install icontract
import icontract
@icontract.require(lambda x: x > 3)
def some_func(x: int) -> None:
pass
some_func(x=5) # succeeds
some_func(x=1) # raises ViolationError with details
Verify before relying
- Performance overhead of contract checking at runtime compared to assertion-based alternatives
- Compatibility with async/await patterns and coroutines
- Whether inheritance contract weakening/strengthening works correctly with multiple inheritance
Package facts
| License | permissive license permissive |
| Python support | Not specified |
| Install friction | Low. Pure-Python wheel |
| Runtime dependencies | 3 packagesasttokenscontextvarstyping_extensions |
| Maintenance | Aging 197 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 408,319 / month, #6,881 on PyPI 30-day window, as of 2026-08-14 |
| Known vulnerabilities | None known OSV.dev, checked 2026-08-14 |
| Classifiers | Development Status :: 5 - Production/StableIntended Audience :: DevelopersProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9 |
Evidence: icontract-2.7.3-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “precondition postcondition decorator”
- icontracticontract enforces design-by-contract in Python through decorators…
- dealDeal is a Python library for design by contract that adds decorators…
- pyamgPyAMG provides Algebraic Multigrid (AMG) solvers for efficiently…
Give your agent the search over MCP, or paste the wish link into any chat.
More Quality Assurance packages
Coverage.py measures which lines of Python code are executed during test runs, reporting coverage percentages and identifying untested code paths.
Install it if you want to measure test completeness or enforce coverage thresholds in your project.
Ruff is a Python linter and code formatter written in Rust that combines linting, formatting, and code fixing into a single tool, replacing Flake8, Black, isort, and related utilities.
Pexpect spawns and controls interactive console applications by sending input and matching output patterns, automating tasks that would otherwise require manual interaction.
Black reformats Python source code to a consistent style by parsing entire files and rewriting them according to an opinionated, deterministic set of rules, eliminating manual formatting decisions.
pytest-xdist distributes pytest tests across multiple CPU cores or machines to speed up test execution, with the simplest usage being `pytest -n auto` to spawn workers equal to available CPUs.
Install it if your test suite takes long enough that parallelization would save meaningful time.
Validates AWS CloudFormation templates in YAML or JSON format against resource provider schemas and best practices, checking property values and configuration correctness.
Install it if you work with CloudFormation templates.
See also deal · crosshair-tool · pact-python · python-interface · overrides · attr · Deprecated · zope.interface · flex · pyvalid