For Python 3 and for the linters sake
def methoddecorator(deco: Callable[[Any, Callable], Callable]):
"""
Decorator to implement method decorators in the same class
Example of usage:
class A:
@methoddecorator
def my_methods_deco(self, method):
@wraps(method)
def wrapper(this: 'A', *args, **kwargs):
# do smth
# N.B. for instance access use this, not self!
return method(this, *args, **kwargs)
return wrapper
@my_methods_deco
def my_method(self, a, b):
...
"""
@functools.wraps(deco)
def wrapped_deco(method):
return deco(NotImplemented, method)
return wrapped_deco
Use this uber-decorator to patch the classes.
BTW, this code does not support decorator parameters like @deco(param=...)
, but more complicated one does.
def methoddecorator(deco):
"""
Decorator to implement method decorators in the same class
Supports optionally parametrized decorators
Example of usage:
class A:
@methoddecorator
def my_methods_deco(self, _method=None, param1=None, param2=None):
@wraps(method)
def wrapper(this: 'A', *args, **kwargs):
# do smth
# deco params are also available here
return method(this, *args, **kwargs)
return wrapper
@my_methods_deco
def my_method1(self, a, b):
...
@my_methods_deco(param1=11, param2=12)
def my_method2(self, a, b):
...
"""
@wraps(deco)
def wrapped_deco(_method=None, **kwargs):
return (
deco(NotImplemented, _method)
if _method is not None
else partial(deco, NotImplemented, **kwargs)
)
return wrapped_deco