Skip to main content

Contracts for Python Functions

Contracts can be used to validate the input or output of a function. Data flow among components can be hard to keep track or maintained, sometimes forcing us to write print statements everywhere trying to catch malformed data. This contracts library solves this by intercepting data that is piped into or out of a function and checking if they satisfy the specified requirements.

Getting Started

Install contracts through pip.

$ pip3 install python-contracts

Once contracts is installed, you can import the input and output contract decorators, and other convenient qualifier functions.

# Imports ic (input contract) and oc (output contract) 
from contracts import ic, oc

# Examples of some qualifiers
from contracts import natural, integer

Refer to Qualifiers for list of available qualifiers.

Contracts

Input contract is a function decorater that takes in kwargs as a mapping of parameter names to their qualifier(s).

Output contract is another decorator that takes in a single qualifier and checks the result of calling the decorated function.

Using Input Contract

from contracts import ic, natural

@ic(val = natural)
def func(val):
    return val

func takes in "val" as a parameter. ic's arguments should correspond to the function's parameter names. In this case, the contract states that arguments for "val" should be natural numbers.

ic can take in as many arguments as the number of parameters for the decorated function. All keys should map to the correct parameter names. If the contract is violated, you will get an InputContractException error message listing out the arguments that failed and their given values.

contracts.exceptions.InputContractException:
Arguments that failed the contracts
val: -1

Using Output Contract

from contracts import oc, natural

@oc(natural)
def func(val):
    return val

oc validates the result of calling a function. In this case, it checks that the result is a natural number. The decorator takes a single qualifier. If the contract is violated, the following error will be given.

contracts.exceptions.OutputContractException: The result of -1 broke the output contract

Qualifiers

Qualifiers are functions that take in a single value and return True if conditions are satisfied, and False otherwise.

For convenience, the contracts library provides a range of basic qualifiers on some data types.

  • Numbers: number, positive_number, negative_number
  • Integers: integer, natural, positive_integer, negative_integer
  • Floats: floating_point, positive_float, negative_float

Composing Multiple Qualifiers

To compose multiple qualifiers, you can import the compose function and pass as many qualifiers as needed to make a single qualifier.

from contracts import natural, positive_integer, compose

@ic(val = compose(natural, positive_integer))
def func(val):
    return val

The input contract above will check that the argument for "val" is a natural number and a positive integer.

Custom Qualifiers

Users of the library can plug in their own qualifiers as long as they adhere to the right data signature. If you're using this library throughout your code base, writing your collection of qualifiers in a separate file will keep things clean.

Built With

Contracts library is built with Python 3.6. Code has been tested only in this version. Compatibility with other versions has not been tested.

Libraries used:

Author

Gino Jacob - Github

Feedback is very much welcomed. Please write issues.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Release files for python-contracts 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-contracts 0.1.4
File Size Uploaded
python-contracts-0.1.4.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-contracts 0.1.4
File Interpreter ABI Platform
python_contracts-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / python-contracts-0.1.4.tar.gz

Download URL python-contracts-0.1.4.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
53a53e0b5fe62f05c4e947bc75e5f2933135b4c06c804f5cf88e0de66baf57f9
BLAKE2b-256 checksum
How to use checksums
1581e436967f34250570e8c2632723f26aec1958f75fef160a9aa0dcff98301d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

Release files / python_contracts-0.1.4-py3-none-any.whl

Download URL python_contracts-0.1.4-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d440913897c7090fabb38d562efdb433bfe0f2cdcd516808fe53e25940d5e12
BLAKE2b-256 checksum
How to use checksums
9a2025d212011451355a8a87bb2c5b299e8ae0e8c77e80ba28e038d1233ac48c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page