MIRROR OF CURRENT PYCONTRACTS ON GITHUBThe version on pypi is stale, this is a mirror where I have only modified the configSee the source here: http://andreacensi.github.com/contracts/
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).
As a quick intro, please see this presentation about PyContracts.
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.
Specifying contracts: Contracts can be specified in three ways:
Using the ``@contract`` decorator:
@contract(a='int,>0', b='list[N],N>0', returns='list[N]') def my_function(a, b): ...
Using annotations (for Python 3):
@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 docstrings, with the :type: and :rtype: tags:
@contract def my_function(a, b): """ Function description. :type a: int,>0 :type b: list[N],N>0 :rtype: list[N] """ ...
Deployment: In production, all checks can be disabled using the function contracts.disable_all(), so the performance hit is 0.
Extensions: You can extend PyContracts with new contracts types:
new_contract('valid_name', lambda s: isinstance(s, str) and len(s)>0) @contract(names='dict(int: (valid_name, int))') def process_accounting(records): ...
Any Python type is a contract:
@contract(a=int, # simple contract b='int,>0' # more complicated ) def f(a, b): ...
Enforcing interfaces: ContractsMeta is a metaclass, like ABCMeta, which propagates contracts to the subclasses:
from contracts import contract, ContractsMeta, with_metaclass class Base(with_metaclass(ContractsMeta, object)): @abstractmethod @contract(probability='float,>=0,<=1') def sample(self, probability): pass class Derived(Base): # The contract above is automatically enforced, # without this class having to know about PyContracts at all! def sample(self, probability): ....
Numpy: There is special support for Numpy:
@contract(image='array[HxWx3](uint8),H>10,W>10') def recolor(image): ...
Status: The syntax is stable and it won’t be changed. PyContracts is very well tested on Python 2.x.
Status on Python 3.x: We reached feature parity! Everything works on Python 3 now.
Contributors:
Chris Beaumont (Harvard-Smithsonian Center for Astrophysics): $var syntax; kwargs/args for extensions.
Brett Graham (Rowland Institute at Harvard University): attr(name:type) syntax for checking types of attributes.
William Furr: bug reports and performance improvements
Karol Kuczmarski (Google Zurich): implementation of “string” and “unicode” contracts
Maarten Derickx (Leiden U.): documentation fixes
Calen Pennington (EdX): disabling checks inside check() function.
Adam Palay (EdX): implementation of environment variable enabling/disabling override.
Ryan Heimbuch: bug reports
Bernhard Biskup: bug reports
asharp: bug fixes
Dennis Kempin (Google mothership): Sphinx-style constraints specs
Andy Hayden: Python 3 support, more efficient Numpy checks
Jonathan Sharpe: contracts for file-like objects, not operator
(Please let me know if I forgot anybody.)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for PyContracts_mirror-2.0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 950ef54b37c9f49501383d57621d9e91ca073c76ae8dc37e59fcb9d8b61c9531 |
|
MD5 | 1f02cfdd84a56778d875cc6e07847ae1 |
|
BLAKE2b-256 | d4061fc17a88cdf831eef9f39d5adeaba25131a8a3fdaa67622405f0b416ea81 |
Hashes for PyContracts_mirror-2.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bacc601ec1dc1337a715fcec61f5e3e3be264c1126a345524d3e96e0ff95ca86 |
|
MD5 | 35f0cfb0ba31624534fb43a3a6b1d6d0 |
|
BLAKE2b-256 | c42ed46f85f7f61036e0374707f6c4d0fd32ba74c304eb27d6d8dba0897c93f5 |
Hashes for PyContracts_mirror-2.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b68ce296877a2b0c25c4b9ccc1f363efb9aead0d8547135f23d6a91e2bc9d24 |
|
MD5 | 6dbc2602cb254beec8f9b2c25e555c9d |
|
BLAKE2b-256 | 368b2529dbc857f232645301bbd333a0619acf89d4d6f45e2b1aff2df65d23d6 |