How to decorate class or static methods
<p>I am writing a generic class decorator which needs to apply a decorator to each method. My first approach is something like this:</p>

<pre><code>def class_decorator(cls):
    for name, member in vars(cls).items():
        # Ignore anything that is not a method
        if not isinstance(member, (types.FunctionType, types.BuiltinFunctionType, classmethod, staticmethod)):
            continue

        setattr(cls, name, method_decorator(member))

    return cls
</code></pre>

<p>The decorator itself is not very important. Looks something like this:</p>

<pre><code>def method_decorator(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        # do something
        return fn(*args, **kwargs):

    return wrapper
</code></pre>

<p>Once I tested it, I ran into the problem that this does not work with static or class methods, and the following error is raised from <code>functools.wraps</code>:</p>

<pre><code>AttributeError: 'classmethod' object has no attribute '__module__'
</code></pre>

<p>Yeah, <code>classmethod</code> or <code>staticmethods</code> are not normal functions, not even callables. Generally if you need to decorate a <code>classmethod</code>, you first apply your decorator and then the <code>classmethod</code> decorator, but since this is a class decorator, I cannot influence the order of the decorators.</p>

<p>Any good solution for this? </p>