Description
The docs (https://github.com/micheles/decorator/blob/master/docs/documentation.md#decoratordecorator) say:
The decorator function can be used as a signature-changing decorator, just like classmethod and staticmethod.
But then goes on to say:
But classmethod and staticmethod return generic objects which are not callable. Instead, decorator returns signature-preserving decorators (i.e. functions with a single argument).
And the following trivial example doesn't appear to change the signature at all?
What I want to do is create a decorator that will add a few optional keyword arguments to the decorated function. At first I thought I should be able to do something like:
@decorator.decorator
def mydec(func, new_kw=None, another_new_kw=None, *args, **kwargs):
if new_kw:
# do something
return func(*args, **kwargs)
But this returns a decorator factory (also useful, but not for me in this case).
Then, I thought I could do it with decorate
, like this:
def _mydec(func, new_kw=None, another_new_kw=None, *args, **kwargs):
if new_kw:
# do something else when the decorated function is executed
return func(*args, **kwargs)
def mydec(func):
newfunc = decorator.decorate(func, _mydec)
# do something else with func when the decorator is created
return newfunc
But this doesn't work. The decorated function still only accepts the original keyword arguments.
I guess the two methods are functionally equivalent, but decorator.decorate
allows to do something with the original function at the time the decorator is created (vs at the time runtime, with decorator.decorator
.
I also found #55 and #58 which seem to imply that signature-changing decorators are not supported at all and go against the entire philosophy of decorator.py
.
But this seems to be a direct contradiction of the quoted docs:
The decorator function can be used as a signature-changing decorator, just like classmethod and staticmethod.
Can I use decorator.py
to create signature-changing decorators, with new required or optional args or keyword args?