[Python-Dev] update_wrapper should preserve staticmethod behavior

Calvin Spealman ironfroggy at socialserve.com
Wed Jun 11 20:56:25 CEST 2008


staticmethod doesn't wrap anything, it just creates a descriptor on  
the class with a __get__ that returns the original, untouched  
callable. Doesn't even care _what_ the thing you use it on is  
(function, other callable, or something else entirely.)

This actually shouldn't be attempted on non-staticmethod descriptors,  
after thinking about it. Can't be sure that desc.__get__(cls) is  
usable to wrap when, at the end, you will be doing some_instance.a  
and now had the wrong __get__() signature used. Oh, no!

class A(object):
	@d
	@some_decorator_returns_a_descriptor
	def a():
		pass

What should probably happen here is that d needs to see its  
decorating a descriptor and itself return a descriptor to pass along  
the right behavior. So, when you do A().a() you should have d.__get__ 
(cls, inst) calling some_decorator_returns_a_descriptor.__get__(cls,  
inst) and acting as if that was the thing it decorated.

Of course, this would have the probably unexpected behavior of  
decorating such things at binding time (ie, when a classmethod is  
bound) rather than after definition. Not good. They could be cached  
and this used to implement new functionality that the decorator can  
be applied to the class method once for each class its bound to  
(interesting? useful?), but I can't think of a justification myself.

Unless any of this other behavior could be justified, I'll provide an  
update_wrapper() patch to at least become staticmethod smart.

On Jun 11, 2008, at 1:48 PM, Antoine Pitrou wrote:

> Calvin Spealman <ironfroggy <at> socialserve.com> writes:
>> 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__'
>
> Well, if staticmethod doesn't mirror the original function's  
> __module__
> attribute, I'd say staticmethod is the culprit.
>
> Since Python grew the update_wrapper function, it seems reasonable  
> to ask
> that all decorators (or decorator-alikes) provided with Python call
> update_wrapper. Of course staticmethod is written in C, so is there a
> C function somewhere providing the same functionality as  
> update_wrapper does?
>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/ 
> ironfroggy%40socialserve.com



More information about the Python-Dev mailing list