Goto Definition for decorated function does not go to decorated function


For the following:
def decoratorFunctionTakesArg(f):
    def wrapped_f(arg):
        print("Before", f.__name__ + "()")
        ret = f(arg)
        print("After", f.__name__ + "()")
        return ret
    return wrapped_f

def returnsGivenWithDecorator(parm):
    return parm

retGivenInt1 = returnsGivenWithDecorator(1)
Right clicking on the use of returnsGivenWithDecorator(1) and choosing 'Goto Definition' (F12) does not take you to the definition of returnsGivenWithDecorator(). Instead, it lists the 'Definition' in the Find Symbol Results window along with a 'Value' which points to the decorator.


pminaev wrote Aug 20, 2013 at 7:25 PM

This is technically correct, though, isn't it? The actual function that is the result of applying the decorator is wrapped_f, so that is what listed as a value. The definition should point at the def statement because that's where the variable was assigned. Thus, the user can decide which one he actually wants.

Zooba wrote Feb 27 at 5:58 PM

This is correct behavior, and I'll note that using @functools.wraps on the wrapper (wrapped_f here) will make the references work correctly. Only if wraps is not used will the value be shown to the user.