PyContracts is a Python package that allows to declare constraints on function parameters and return values. Contracts can be specified using Python3 annotations, or inside a docstring. PyContracts supports a basic type system, variables binding, arithmetic constraints, and has several specialized contracts and an extension API.
#!/usr/bin/env python
from contracts import contract
@contract
def foo(bar, baz):
'''
:param bar: A thing.
:type bar: str
:param baz: Another thing.
'''
print bar, baz
if __name__ == '__main__':
foo('hello', 'world')
The traceback is:
Traceback (most recent call last):
File "foo.py", line 5, in <module>
@contract
File "/Users/geordan/src/contracts/src/contracts/main.py", line 166, in contract_decorator
return contracts_decorate(function, **kwargs)
File "/Users/geordan/src/contracts/src/contracts/main.py", line 228, in contracts_decorate
accepts_dict, returns = parse_contracts_from_docstring(function)
File "/Users/geordan/src/contracts/src/contracts/main.py", line 362, in parse_contracts_from_docstring
for name in params])
File "/Users/geordan/src/contracts/src/contracts/main.py", line 347, in remove_quotes
if x.startswith('``') and x.endswith('``') and len(x) > 3:
AttributeError: 'NoneType' object has no attribute 'startswith'
The following script will produce an error:
The traceback is: