[Python-Dev] update_wrapper should preserve staticmethod behavior
Guido van Rossum
guido at python.org
Wed Jun 11 19:39:26 CEST 2008
Please submit a fix to the issue tracker at bugs.python.org if you
care about this.
On Wed, Jun 11, 2008 at 7:06 AM, Calvin Spealman
<ironfroggy at socialserve.com> wrote:
> I'd like to make a claim about the following example, that update_wrapper
> should be improved to preserve the behavior of known, built-in decorators.
> In this case, I'm talking about staticmethod. The order I list here feels
> natural, but it obviously doesn't work. The only reason it doesn't seems to
> be that it is trying to decorate the descriptor, not the function itself.
> This is expected, but it could certainly be smart enough to detect a
> descriptor and attempt to get the actual callable underneath, could it not?
> It would not work for all cases, of course.
>>>> def d(f):
> ... def nf(*a, **kw):
> ... print "decorated function called"
> ... return f(*a, **kwargs)
> ... functools.update_wrapper(nf, f)
> ... return nf
>>>> class A(object):
> ... @d
> ... @staticmethod
> ... def a(self):
> ... print "a"
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 3, in A
> File "<stdin>", line 5, in d
> line 33, in update_wrapper
> setattr(wrapper, attr, getattr(wrapped, attr))
> AttributeError: 'staticmethod' object has no attribute '__module__'
> Python-Dev mailing list
> Python-Dev at python.org
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev