skillfed

icontract

Provide design-by-contract with informative violation messages.

icontract v2.7.3 408.3K downloads/30d#6,881 on PyPI411
Permissive license License :: OSI Approved :: MIT License AGING released

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

icontract enforces design-by-contract in Python through decorators that validate preconditions, postconditions, and class invariants, with detailed violation messages showing source code and variable values.

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

pip

pip install icontract

uv

uv add icontract

poetry

poetry add icontract

Installing icontract

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 License :: OSI Approved :: MIT License (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies 3 — asttokens, contextvars, typing_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)

Evidence: icontract-2.7.3-py3-none-any.whl

Keywords: design-by-contract, precondition, postcondition, validation

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

Tags

design by contract pythonprecondition postcondition decoratorcontract validation enforcementpython code contractsassertion with detailed messagesinvariant checking decoratorruntime contract verification
design-by-contractruntime-validationapi-documentation

More Quality Assurance packages