Skip to main content

Function contracts for Python

Project description

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

Note: To use both ic and oc, oc needs to be placed above ic as such:

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

Otherwise, your code will break. This is definitely something to improve on.

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

Project details


Download files

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

Source Distribution

python-contracts-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_contracts-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file python-contracts-0.1.1.tar.gz.

File metadata

  • Download URL: python-contracts-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for python-contracts-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a7b25b82b4d82f33f5abfae1c5d05726806ccff12f1b5b2a975b8478a8c5573
MD5 48c6c410a5b3adf08f9a55a17f8350d9
BLAKE2b-256 7416b7e87d5177bf301e237ec2d3c6980bfedfbf5dd3cc51411e365ef9623433

See more details on using hashes here.

File details

Details for the file python_contracts-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: python_contracts-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for python_contracts-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87b57139df2879f4fb7978af9c673cfda9652dd8d2245814c341a78e04f0145a
MD5 fe5f9fadaaecbe2029d782c61d3bf51b
BLAKE2b-256 ba4cd69391691a04d6c2fbb8e182c68144e94a13a872164dcc0ce7222fc9eb25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page