adventurelib icon indicating copy to clipboard operation
adventurelib copied to clipboard

@when does not play nicely with other decorators.

Open flosincapite opened this issue 3 years ago • 3 comments

I would like to be able to do something like this:

@adventurelib.when("command")
@other_decorator
def command():
    pass

If I want other_decorator to capture the args passed to command for some reason, I have to implement other_decorator like this:

def other_decorator():
    def dec(func):
        def wrapped(*args, **kwargs):
            # do something with args, kwargs
            return func(*args, **kwargs)
        return wrapped
    return dec

However, adventurelib.when prevents this, since the signature of wrapped(*args, **kwargs) differs from the signature of command().

Ideally, the signature validation logic in _register would be able to detect that signatures are compatible, even if they're not identical. Failing that, can you add a mechanism to disable signature enforcement?

Also note that changing the order of decorators won't work, e.g.

@other_decorator
@when(...)
def command():
    ...

will not incorporate other_decorator into the registered function.

flosincapite avatar Dec 08 '20 03:12 flosincapite

For what it's worth, I developed a workaround, but it involves exec and is very, very ugly.

flosincapite avatar Dec 08 '20 22:12 flosincapite