Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

A library that provides code contracts and assertions.

Project Description

code-contracts is a library that provides:

  1. Contracts
    Functions that impose requirements when entering a function or method - also commonly called preconditions.
  2. Assertions
    Functions that impose requirements in the body of a function or method. This is not a complete suite of assertions; instead, they are meant to be complementary to those available in unittest.TestCase.

The goal of this library is not to make Python a statically-typed language. Instead, it aims to help you define what is expected from your code before it executes, so that you can more easily track and prevent bugs.

So here’s how you can use contracts:

from contracts import contract

def build_rocket(name, model, company):
    contract.is_greater_than(model, 0)

    print("You built a {0} {1} rocket from {2}.".format(name, model, company))

if __name__ == "__main__":
    build_rocket("Falcon", 9, "SpaceX")

And here’s how you can use assertions to unit test the above function:

import unittest
from contracts import assertion

class RocketTests(unittest.TestCase):
    def test_build_rocket(self):
        assertion.does_not_raise(ValueError, build_rocket, "Falcon", 9, "SpaceX")

code-contracts officially supports Python 3.3 and onwards.


Simply run the following command in your favorite terminal:

$ pip install code-contracts


The full documentation is available at

Release History

This version
History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(7.7 kB) Copy SHA256 Hash SHA256
py3 Wheel Aug 31, 2017
(5.6 kB) Copy SHA256 Hash SHA256
Source Aug 31, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting