Domanda

In the following example code below, we have an undecorated function fun() and a decorated one wrappedfun().

For the undecorated function fun, pressing TAB in IPython notebook after the opening parenthesis following the function name shows the call signature as fun(x=0, y=1) but for the decorated version wrappedfun you get the completion as wrappedfun(*args, **kwargs). Is there any way to tell IPython to display the original call signature during TAB completion?

from functools import wraps

def mywrapper(func):
''' simple wrapper '''
    @wraps(func)
    def wrapper(*args, **kwargs):
        print('inside wrapper')
        return func(*args, **kwargs)
    return wrapper

def fun(x=0, y=1):
    ''' Docstring for fun '''
    return x + y

@mywrapper
def wrappedfun(x=0, y=1):
    ''' Docstring for wrapped another fun '''
    return x + y
È stato utile?

Soluzione

I think the problem lie in there :

def wrapper(*args, **kwargs):
    print('inside wrapper')
    return func(*args, **kwargs)

even if it @wraps(func) , wrapper might take more arguments, so the signature you see is the signature of wrapper. There is no way to magically know what you want to do.

This will not be specific to IPython, and can be found elsewhere on stack overflow like Preserving signature of decorated function. Hope this will solve your issue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top