AndreaCensi / contracts

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.
http://andreacensi.github.io/contracts/
Other
399 stars 61 forks source link

Support checking both type annotations and docstring contracts #79

Open AaronCritchley opened 5 years ago

AaronCritchley commented 5 years ago

Hey,

When using contracts, we often would like to provide type hints to help the IDE, whilst having more complex contracts in the docstring to be checked. Can I add support for this to PyContracts? I'm happy if it falls behind a configuration flag or something similar.

An over simplified example could be:

@contract
def add_gt_5(x: int, y: int):
    """
    Add two numbers that are both greater than 5
    :type x: int, >5
    :type y: int, >5
    :rtype: int, >11
    """
    return x + y

add_gt_5(10, 10)  # Fine
add_gt_5(2, 2)  # Should fail

I'm aware we can specify contracts as strings in the type annotations but then the IDE cannot decipher types to make life easier for the user.

Again, I'm happy to make this change just want to check that you're happy with it being in master. 😄