Skip to main content

PyContracts is a Python package that allows to declare constraints on function parameters and return values. Contracts can be specified using Python3 annotations, in a decorator, or inside a docstring :type: and :rtype: tags. PyContracts supports a basic type system, variables binding, arithmetic constraints, and has several specialized contracts (notably for Numpy arrays), as well as an extension API.

Project description

PyContracts is a Python package that allows to declare constraints on function parameters and return values. It supports a basic type system, variables binding, arithmetic constraints, and has several specialized contracts (notably for Numpy arrays).

A brief summary follows. See the full documentation at: <http://andreacensi.github.com/contracts/>

Why: The purpose of PyContracts is not to turn Python into a statically-typed language (albeit you can be as strict as you wish), but, rather, to avoid the time-consuming and obfuscating checking of various preconditions. In fact, more than the type constraints, I found useful the ability to impose value and size constraints. For example, “I need a list of at least 3 positive numbers” can be expressed as list[>=3](number, >0)). If you find that PyContracts is overkill for you, you might want to try a simpler alternative, such as typecheck. If you find that PyContracts is not enough for you, you probably want to be using Haskell instead of Python.

Contracts can be specified in three ways:

  • Using annotations (for Python 3) —this is perhaps the most intuitive way:

    @contract
    def my_function(a : 'int,>0', b : 'list[N],N>0') -> 'list[N]':
         # Requires b to be a nonempty list, and the return
         # value to have the same length.
         ...
  • Using :type: and :rtype: tags in docstrings. In this way, they will be included in your Sphinx documentation:

    @contract
    def my_function(a, b):
        """ Function description.
            :type a: int,>0
            :type b: list[N],N>0
            :rtype: list[N]
        """
        ...
  • Using arguments to the decorators; the least intrusive way:

    @contract(a='int,>0', b='list[N],N>0', returns='list[N]')
    def my_function(a, b):
        ...

Moreover, there are utility functions for manual checking of values:

check('array[HxWx3](uint8),H>10,W>10', image)

as well as hooks to extend PyContracts with new contracts types:

new_contract('valid_name', lambda s: isinstance(s, str) and len(s)>0)
check('dict(int: (valid_name, int))', employees)

Status: PyContracts is very well tested and documented. The syntax is stable and it won’t be changed.

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

PyContracts-1.5.1.tar.gz (43.1 kB view details)

Uploaded Source

File details

Details for the file PyContracts-1.5.1.tar.gz.

File metadata

  • Download URL: PyContracts-1.5.1.tar.gz
  • Upload date:
  • Size: 43.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyContracts-1.5.1.tar.gz
Algorithm Hash digest
SHA256 22858100fbd9e11e9b6bded66bf154d64ce4bf68f8991dde969db0467907a406
MD5 36845c5b61b498a3c574c78a7e3b2c2c
BLAKE2b-256 931e268de53e3d58aaf3578d782be1178896d65d0a9b1e3769ce848ce47a357a

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