I am trying to write a decorator that stores the arguments of the functions that it decorates. The motivation for this is to write a decorator that interacts well with pytest.fixtures .
Suppose we have a function foo . It takes one argument a .
def foo(a): pass
If we get the argument spec foo
>>> inspect.getargspec(foo) ArgSpec(args=['a'], varargs=None, keywords=None, defaults=None)
We often want to create a decorator where the wrapper function passes all its arguments verbatim to the wrapped function. The most obvious way to do this is using *args and **kwargs .
def identity_decorator(wrapped): def wrapper(*args, **kwargs): return wrapped(*args, **kwargs) return wrapper def identity_decorator(wrapped): def wrapper(*args, **kwargs): return wrapped(*args, **kwargs) return wrapper @identity_decorator def foo(a): pass
This, unsurprisingly, creates a function with an argument specification that reflects *args and **kwargs .
>>> inspect.getargspec(foo) ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None)
Is there a way to change the argument specification to match the completed function, or create a function with the correct argument specification initially?
source share