0

Below is a MWE of my attempt to change the signature of MyClass.func from (self, a, b, c) to (self, x, y, z). As you can see from the outputs, the change_sig decorator works on functions and also works on the bound method MyClass().func, but fails on the unbound method. Is there some sort of magic going on behind the scenes when I assign a callable attribute to a class that keeps this from working?

MWE:

import wrapt
import inspect

def change_sig(func):
    adapter = '(self, x, y, z)'
    @wrapt.decorator(adapter=adapter)
    def wrapper(wrapped, instance, args, kwargs):
        pass
    wrapped_func = wrapper(func)
    return wrapped_func

@change_sig
def func(self, a, b, c):
    pass

class MyClass:
    @change_sig
    def func(self, a, b, c):
        pass

print('inspect.signature(func):', inspect.signature(func))
print('inspect.signature(MyClass.func):', inspect.signature(MyClass.func))
print('inspect.signature(MyClass().func):', inspect.signature(MyClass().func))

print('inspect.signature(change_sig(MyClass.func)):', inspect.signature(change_sig(MyClass.func)))
MyClass.wrapped_func = change_sig(MyClass.func)
print('inspect.signature(MyClass.wrapped_func):', inspect.signature(MyClass.wrapped_func))

Output:

inspect.signature(func): (self, x, y, z)
inspect.signature(MyClass.func): (self, a, b, c)
inspect.signature(MyClass().func): (x, y, z)
inspect.signature(change_sig(MyClass.func)): (self, x, y, z)
inspect.signature(MyClass.wrapped_func): (self, a, b, c)

1 Answers1

0

This was traced to a bug in wrapt, which is being addressed: https://github.com/GrahamDumpleton/wrapt/issues/148