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
398 stars 62 forks source link

Variable binding in new_contract #56

Open darrnshn opened 7 years ago

darrnshn commented 7 years ago

With the following code:

from contracts import contract, new_contract

new_contract('vector', 'list[N]')

@contract
def foo(X: 'vector', y: 'vector'):
    pass

foo([1, 2, 3], [1, 2])

I would've expected it to fail since the two lists have different lengths, but it doesn't throw an error. However, if I don't use new_contract, it works. Is this the intended behaviour? If so, how would I get the behaviour that I want? I'm using version 1.7.15.

AndreaCensi commented 6 years ago

Sorry, I can see how it is confusing.

This is a known limitation - variable binding does not cross "new_contract" behavior.

I'd welcome a patch to fix this.