6

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.

3
  • The returned FunctionWrapper class doesn't know how to be properly bound to the instance (it is missing the required __get__ method, see Invoking Descriptions). Commented Aug 28, 2012 at 0:19
  • That's because the decorated function is no longer a function but a callable object. BTW, this function implementation looks quite bad (and unpythonic)... Commented Aug 28, 2012 at 0:20
  • I think it's rather because you decorate method - not just function - If I were you then I would just use my own decorator. Commented Aug 28, 2012 at 0:35

2 Answers 2

14

You don't need to do anything special:

import functools

def condition(pre_condition=None, post_condition=None):
    def decorator(func):
        @functools.wraps(func) # presever name, docstring, etc
        def wrapper(*args, **kwargs): #NOTE: no self
            if pre_condition is not None:
               assert pre_condition(*args, **kwargs)
            retval = func(*args, **kwargs) # call original function or method
            if post_condition is not None:
               assert post_condition(retval)
            return retval
        return wrapper
    return decorator

def pre_condition(check):
    return condition(pre_condition=check)

def post_condition(check):
    return condition(post_condition=check)

Usage:

@pre_condition(lambda arg: arg > 0)
def function(arg): # ordinary function
    pass

class C(object):
    @post_condition(lambda ret: ret > 0)
    def method_fail(self):
        return 0
    @post_condition(lambda ret: ret > 0)
    def method_success(self):
        return 1

Test:

function(1)
try: function(0)
except AssertionError: pass
else: assert 0, "never happens"

c = C()
c.method_success()
try: c.method_fail()
except AssertionError: pass
else: assert 0, "never happens"
Sign up to request clarification or add additional context in comments.

Comments

0

example below works:

def out_gt0(retval):
    assert retval > 0, "Return value < 0"

def mypostfunc(callback):
    def mydecorator(func):
        def retFunc(self, *args, **kwargs):
            retval = func(self, *args, **kwargs)
            callback(retval)
            return retval
        return retFunc
    return mydecorator

class foo(object):
    def __init__(self, w, h):
        self.width = w
        self.height = h
    @mypostfunc(out_gt0)
    def bar1(self):
        return -1
    @mypostfunc(out_gt0)
    def bar2(self):
        return 1

f=foo(1,2)
print "bar2:", f.bar2()
print "bar1:", f.bar1()

and the output is:

bar2: 1
bar1:
Traceback (most recent call last):
  File "s.py", line 27, in <module>
    print "bar1:", f.bar1()
  File "s.py", line 9, in retFunc
    callback(retval)
  File "s.py", line 3, in out_gt0
    assert retval > 0, "Return value < 0"
AssertionError: Return value < 0

2 Comments

Here you got nice way - if you want to run your example, just run f.bar(f) and it should also work (if f.bar will have arguments, then always push that object on the first position - this will be self in your function)
The reason this works is that now the decorator is a function object, which does implement a __get__ descriptor hook.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.