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

Allow args/kwargs to be passed to Extension contracts #28

Closed ChrisBeaumont closed 9 years ago

ChrisBeaumont commented 9 years ago

Hi @AndreaCensi

Some more experimentation on my end: this change would allow you to pass args/kwargs into custom contracts:

@new_contract
def greater_than(value, thresh):
    return value > thresh

parse('greater_than(3)').check(4)
parse('greater_than(3)').fail(3)

This provides a way for users to implement missing/undocumented features like #23, and would also address the issue I raised in #26

@new_contract
def t(value, typ):
    return isinstance(value, typ)

parse('t($Foo)')

Thoughts?

AndreaCensi commented 9 years ago

Hi Chris, I'll be traveling for the next few days. I'll answer when I get back towards the end of the week. A.

On Mon, Nov 17, 2014 at 3:42 PM, Chris Beaumont notifications@github.com wrote:

Hi @AndreaCensi https://github.com/AndreaCensi

Some more experimentation on my end: this change would allow you to pass args/kwargs into custom contracts:

@new_contractdef greater_than(value, thresh): return value > thresh

parse('greater_than(3)').check(4) parse('greater_than(3)').fail(3)

This provides a way for users to implement missing/undocumented features like #23 https://github.com/AndreaCensi/contracts/issues/23, and would also address the issue I raised in #26 https://github.com/AndreaCensi/contracts/issues/26

@new_contractdef t(value, typ): return isinstance(value, typ)

parse('t($Foo)')

Thoughts?

You can merge this Pull Request by running

git pull https://github.com/ChrisBeaumont/contracts multiarg

Or view, comment on, or merge it at:

https://github.com/AndreaCensi/contracts/pull/28 Commit Summary

  • Allow args/kwargs to be passed to Extension contracts

File Changes

Patch Links:

— Reply to this email directly or view it on GitHub https://github.com/AndreaCensi/contracts/pull/28.

Andrea Censi | LIDS / MIT | http://censi.mit.edu

AndreaCensi commented 9 years ago

Anyway it looks like a good idea.

On Mon, Nov 17, 2014 at 8:57 PM, Andrea Censi censi@mit.edu wrote:

Hi Chris, I'll be traveling for the next few days. I'll answer when I get back towards the end of the week. A.

On Mon, Nov 17, 2014 at 3:42 PM, Chris Beaumont notifications@github.com wrote:

Hi @AndreaCensi https://github.com/AndreaCensi

Some more experimentation on my end: this change would allow you to pass args/kwargs into custom contracts:

@new_contractdef greater_than(value, thresh): return value > thresh

parse('greater_than(3)').check(4) parse('greater_than(3)').fail(3)

This provides a way for users to implement missing/undocumented features like #23 https://github.com/AndreaCensi/contracts/issues/23, and would also address the issue I raised in #26 https://github.com/AndreaCensi/contracts/issues/26

@new_contractdef t(value, typ): return isinstance(value, typ)

parse('t($Foo)')

Thoughts?

You can merge this Pull Request by running

git pull https://github.com/ChrisBeaumont/contracts multiarg

Or view, comment on, or merge it at:

https://github.com/AndreaCensi/contracts/pull/28 Commit Summary

  • Allow args/kwargs to be passed to Extension contracts

File Changes

Patch Links:

— Reply to this email directly or view it on GitHub https://github.com/AndreaCensi/contracts/pull/28.

Andrea Censi | LIDS / MIT | http://censi.mit.edu

Andrea Censi | LIDS / MIT | http://censi.mit.edu

ChrisBeaumont commented 9 years ago

@AndreaCensi the __str__ doesn't properly embed args or kwargs yet:

In [1]: from contracts import new_contract, parse

In [2]: @new_contract
def foo(value, x, y, z):
    pass
   ...: 

In [3]: c = parse('foo(1,2,3)')

In [4]: c   #ok
Out[4]: Extension('foo', args=(SimpleRValue(1), SimpleRValue(2), SimpleRValue(3)), kwargs={})

In [5]: print c  # incomplete
foo

I think I can fix this easily, but were the unit tests supposed to catch it?

AndreaCensi commented 9 years ago

I think I can fix this easily, but were the unit tests supposed to catch it?

Yes they should have --- every contract is pickled, unpickled, compared with its serialization, rewritten, reparsed, etc.

Uhmmm....

On Fri, Nov 21, 2014 at 11:01 AM, Chris Beaumont notifications@github.com wrote:

@AndreaCensi https://github.com/AndreaCensi the str doesn't properly embed args or kwargs yet:

In [1]: from contracts import new_contract, parse

In [2]: @new_contract def foo(value, x, y, z): pass ...:

In [3]: c = parse('foo(1,2,3)')

In [4]: c #ok Out[4]: Extension('foo', args=(SimpleRValue(1), SimpleRValue(2), SimpleRValue(3)), kwargs={})

In [5]: print c # incomplete foo

I think I can fix this easily, but were the unit tests supposed to catch it?

— Reply to this email directly or view it on GitHub https://github.com/AndreaCensi/contracts/pull/28#issuecomment-63991082.

Andrea Censi | LIDS / MIT | http://censi.mit.edu

AndreaCensi commented 9 years ago

Well actually we didn't put any test about this.

See good(), fail() and syntax_fail() use in many of the *_tc.py files.

AndreaCensi commented 9 years ago

I'm writing some tests.

AndreaCensi commented 9 years ago

All right, we have some failures in the tests now. See src/contracts/testing/library/extensions_tc.py committed in master.

AndreaCensi commented 9 years ago

Just committed a fix to the tests. Now the only tests failing are showing the bug you found.

AndreaCensi commented 9 years ago

All tests running in master. Case closed.