<div dir="ltr"><div dir="ltr"><div><div><div><div><div><div><div><div><div>Hi Cameron,<br><br></div>Just a word of caution: I made a mistake and badly designed interface to icontract. It will change in the near future from:<br><br></div>@post(lambda arg1, arg2, result: arg1 < result < arg2)<br><br></div>most probably to:<br><br></div>@ensures(lambda P:  P.arg1 < result < P.arg2)<br><br></div>This avoids any name conflicts with "result" if it's in the arguments and also many conflicts with web frameworks which frequently use "post". We will also add snapshotting before the function execution:<br></div>@snapshot(lambda P, var2: set(arg2))<br></div>@ensures(lambda O, P: P.arg1 < result and result in O.var2)<br><br></div>so that postcondition can deal with state transitions. There are also some other approaches in discussion.<br><br></div>The library name will also need to change. When I started developing it, I was not aware of Java icontract library. It will be probably renamed to "pcontract" or any other suggested better name :)<br><div><div><br><div><div>Please see the github issues for more details and current discussions: <a href="https://github.com/Parquery/icontract/issues">https://github.com/Parquery/icontract/issues</a> <br></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, 30 Sep 2018 at 06:44, Cameron Simpson <<a href="mailto:cs@cskk.id.au">cs@cskk.id.au</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 30Sep2018 12:17, Chris Angelico <<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>> wrote:<br>
>At the moment, I'm seeing decorator-based contracts as a clunky<br>
>version of unit tests. We already have "inline unit testing" - it's<br>
>called doctest - and I haven't seen anything pinned down as "hey, this<br>
>is what it'd take to make contracts more viable". Certainly nothing<br>
>that couldn't be done as a third-party package. But I'm still open to<br>
>being swayed on that point.<br>
<br>
Decorator based contracts are very little like clunky unit tests to me. I'm <br>
basing my opinion on the icontracts pip package, which I'm going to start <br>
using.<br>
<br>
In case you've been looking at something different, it provides a small number <br>
of decorators including @pre(test-function) and @post(test-function) and the <br>
class invariant decorator @inv, with good error messages for violations.<br>
<br>
They are _functionally_ like putting assertions in your code at the start and <br>
end of your functions, but have some advantages:<br>
<br>
- they're exposed, not buried inside the function, where they're easy to see <br>
  and can be considered as contracts<br>
<br>
- they run on _every_ function call, not just during your testing, and get <br>
  turned off just like assertions do: when you run Python with the -O <br>
  (optimise) option. (There's some more tuning available too.)<br>
<br>
- the assertions make qualitative statements about the object/parameter state <br>
  in the form "the state is consistent if these things apply";<br>
  tests tend to say "here's a situation, do these things and examine these <br>
  results". You need to invent the situations and the results, rather than <br>
  making general statements about the purpose and functional semantics of the <br>
  class.<br>
<br>
They're different to both unit tests _and_ doctests because they get exercised <br>
during normal code execution. Both unit tests and doctests run _only_ during <br>
your test phase, with only whatever test scenarios you have devised.<br>
<br>
The difficulty with unit tests and doctests (both of which I use) and also <br>
integration tests is making something small enough to run but big/wide enough <br>
to cover all the relevant cases. They _do not_ run against all your real world <br>
data. It can be quite hard to apply them to your real world data.<br>
<br>
Also, all the @pre/@post/@inv stuff will run _during_ your unit tests and <br>
doctests as well, so they get included in your test regime for free.<br>
<br>
I've got a few classes which have a selftest method whose purpose is to confirm <br>
correctness of the instance state, and I call that from a few methods at start <br>
and end, particularly those for which unit tests have been hard to write or I <br>
know are inadequately covered (and probably never will be because devising a <br>
sufficient test case is impractical, especially for hard to envisage corner <br>
cases).<br>
<br>
The icontracts module will be very helpful to me here: I can pull out the <br>
self-test function as the class invariant, and make a bunch of @pre/@post <br>
assertions corresponding the the method semantic definition.<br>
<br>
The flip side of this is that there's no case for language changes in what I <br>
say above: the decorators look pretty good to my eye.<br>
<br>
Cheers,<br>
Cameron Simpson <<a href="mailto:cs@cskk.id.au" target="_blank">cs@cskk.id.au</a>><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>