status of Programming by Contract (PEP 316)?

Ryan Ginstrom software at ginstrom.com
Thu Aug 30 20:23:47 EDT 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.
http://oakwinter.com/code/typecheck/

>>> from typecheck import accepts, returns, Any
>>> @accepts(str, int, Any())
@returns(list)
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)
  File
"C:\Python25\lib\site-packages\typecheck-0.3.5-py2.5.egg\typecheck\__init__.
py", line 1340, in fake_function
  File
"C:\Python25\lib\site-packages\typecheck-0.3.5-py2.5.egg\typecheck\__init__.
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.

Regards,
Ryan Ginstrom




More information about the Python-list mailing list