[Python-ideas] Why is design-by-contracts not widely adopted?

Barry Scott barry at barrys-emacs.org
Mon Sep 24 15:34:47 EDT 2018



> On 24 Sep 2018, at 20:09, Marko Ristin-Kaufmann <marko.ristin at gmail.com> wrote:
> 
> Hi Barry,
> I think the main issue with pyffel is that it can not support function calls in general. If I understood it right, and Angus please correct me, you would need to wrap every function that you would call from within the contract.
> 
> But the syntax is much nicer than icontract or dpcontracts (see these packages on pypi). What if we renamed "args" argument and "old" argument in those libraries to just "a" and "o", respectively? Maybe that gives readable code without too much noise:

The args and old and not noise its easier to read the a and o.
a and o as aliases for more descriptive names maybe, but not as the only name.

> 
> @requires(lambda self, a, o: self.sum == o.sum - a.amount)
> def withdraw(amount: int) -> None:
>     ...
> 
> There is this lambda keyword in front, but it's not too bad?

The lambda smells of internals that I should not have to care about being exposed.
So -1 on lambda being required.
Also being able to supply a list of conditions was a +1.

> 
> I'll try to contact dpcontracts maintainers. Maybe it's possible to at least merge a couple of libraries into one and make it a de facto standard. @Agnus, would you also like to join the effort?
> 
> Cheers,
> Marko
> 
> 
> 
> 
> 
> Le lun. 24 sept. 2018 à 19:57, Barry Scott <barry at barrys-emacs.org <mailto:barry at barrys-emacs.org>> a écrit :
> 
> 
>> On 23 Sep 2018, at 11:13, Angus Hollands <goosey15 at gmail.com <mailto:goosey15 at gmail.com>> wrote:
>> 
>> Hi Marko,
>> 
>> I think there are several ways to approach this problem, though am not weighing in on whether DbC is a good thing in Python. I wrote a simple implementation of DbC which is currently a run-time checker. You could, with the appropriate tooling, validate statically too (as with all approaches). In my approach, I use a “proxy” object to allow the contract code to be defined at function definition time. It does mean that some things are not as pretty as one would like - anything that cannot be hooked into with magic methods i.e isinstance, but I think this is acceptable as it makes features like old easier. Also, one hopes that it encourages simpler contract checks as a side-effect. Feel free to take a look - https://github.com/agoose77/pyffel <https://github.com/agoose77/pyffel>
>> It is by no means well written, but a fun PoC nonetheless.
>> 
> This is an interesting PoC, nice work! I like that its easy to read the tests.
> 
> Given a library like this the need to build DbC into python seems unnecessary.
> 
> What do other people think?
> 
> Barry
> 
> 
> 
>> Regards,
>> Angus
>> 
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org <mailto:Python-ideas at python.org>
>> https://mail.python.org/mailman/listinfo/python-ideas <https://mail.python.org/mailman/listinfo/python-ideas>
>> Code of Conduct: http://python.org/psf/codeofconduct/ <http://python.org/psf/codeofconduct/>
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org <mailto:Python-ideas at python.org>
> https://mail.python.org/mailman/listinfo/python-ideas <https://mail.python.org/mailman/listinfo/python-ideas>
> Code of Conduct: http://python.org/psf/codeofconduct/ <http://python.org/psf/codeofconduct/>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180924/83edecca/attachment-0001.html>


More information about the Python-ideas mailing list