status of Programming by Contract (PEP 316)?

Ryan Ginstrom software at
Fri Aug 31 02:23:47 CEST 2007

> On Behalf Of Russ
> Once you have the conditions in place, all you need to do in 
> your unit tests is to send inputs to the unit and wait to see 
> if exceptions are thrown.

That sounds a little ambitious to me...

However, you may want to look at the typecheck module (you can get it via
easy_install). I think you could probably extend it to do DBC.

>>> from typecheck import accepts, returns, Any
>>> @accepts(str, int, Any())
def makelist(a, b, c):
	return [a, b, c]

>>> makelist("spam", 42, object())
['spam', 42, <object object at 0x00AE0470>]
>>> makelist(42, "spam", 3.4)

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    makelist(42, "spam", 3.4)
py", line 1340, in fake_function
py", line 1419, in __check_args
TypeCheckError: Argument a: for 42, expected <type 'str'>, got <type 'int'>

I tried using DBC for a time in C++ (using a library with a clever
assembly-language hack). I personally found it neater having such code in
unit tests, but obviously, it's a matter of preference.

Ryan Ginstrom

More information about the Python-list mailing list