[Python-Dev] update_wrapper should preserve staticmethod behavior

Calvin Spealman ironfroggy at socialserve.com
Wed Jun 11 16:06:35 CEST 2008


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
   File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/functools.py", line 33, in update_wrapper
     setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'staticmethod' object has no attribute '__module__'


More information about the Python-Dev mailing list