I want to list the arguments of my methods for a self documenting REST API. I have found that I can get the arguments of a method using:
method.__code__.co_varnames[:method.__code__.co_argcount]
However, this does not work when the method is decorated.
class Rator:
def __init__(self):
pass
def __call__(self, func):
def wrapper(instance, **kwargs):
func(instance, **kwargs)
return wrapper
class Klass:
def method(self, var_one=None, var_two=None):
pass
@Rator()
def decorated_method(self, var_one=None, var_two=None):
pass
if __name__ == '__main__':
klass = Klass()
print("method args is " + str(klass.method.__code__.co_varnames))
print("decorated method args is " + str(klass.decorated_method.__code__.co_varnames))
Outputs
method args is ('self', 'var_one', 'var_two')
decorated method args is ('instance',)
A solution that does not require change of the decorator is preferred.
I know that this question is a duplicate of How to retrieve method arguments of a decorated python method, but it has since long been dead.