I am using a class decorator for a subclass TestClass which inherits SuperClass. I have a classmethod in SuperClass called what(cls) which takes in a class. I want to be able to decorate that class in my subclass TestClass, but it is not letting me as it is saying.
TypeError: unbound method wrapper() must be called with TestClass instance as first argument (got nothing instead)
I have tried to make an instantiation of my TestClass object and then use that to call method testclass.what(cls)
and that works, but when I do TestClass.what()
, it gives me the error above.
def class_decorator(cls):
for attr_name in dir(cls):
attr_value = getattr(cls, attr_name)
if hasattr(attr_value, '__call__'): # check if attr is a function
# apply the function_decorator to your function
# and replace the original one with your new one
setattr(cls, attr_name, ball(attr_value))
return cls
def ball(func):
def wrapper(*args, **kwargs):
print("hello")
return func(*args, **kwargs)
return wrapper
class SuperClass:
def __init__(self):
pass
@classmethod
def what(cls):
print("testing")
@class_decorator
class TestClass(SuperClass):
def what(cls):
super().what()
TestClass.what()
Expected:
"hello"
"testing"
"hello"
"testing"
Actual: TypeError: unbound method wrapper() must be called with TestClass instance as first argument (got nothing instead)