I'm trying to use the @postcondition decorator on the value returned by a member function of a class, like this :
def out_gt0(retval, inval):
assert retval > 0, "Return value < 0"
class foo(object):
def __init__(self, w, h):
self.width = w
self.height = h
@postcondition(out_gt0)
def bar(self):
return -1
When I try to call the member function 'bar' (and so provoke the @postcondition into providing a warning) I get this :
>>> f = foo(2,3)
>>> f.bar()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
f.bar()
File "<pyshell#8>", line 106, in __call__
result = self._func(*args, **kwargs)
TypeError: bar() takes exactly 1 argument (0 given)
>>>
My definition of @postcondition is the one seen here http://wiki.python.org/moin/PythonDecoratorLibrary#Pre-.2FPost-Conditions.
I assume the error arises because the function that underlies @postcondition is not expecting to deal with a member function (certainly all the examples I've ever seen are just using plain old functions) but I'm not sure how to fix it so I can do this ?
Would be grateful for any advice.
FunctionWrapperclass doesn't know how to be properly bound to the instance (it is missing the required__get__method, see Invoking Descriptions).