Fix naive introspection when using Django decorators
|Reported by:||Jeremy Dunck||Owned by:||Gary Wilson|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Description (last modified by )
It's fairly well-known that decorators are useful, but raise some issues.
def decorate(f): def wrap(*args, **kwargs): print "called" return f(*args, **kwargs) return wrap @decorate def add_to(augend, addend): "Adds stuff" return augend + addend
Introspecting add_to, undecorated, would have a
__name__ of 'add_to'
__doc__ of 'Adds stuff'.
add_to.__name__ becomes 'wrap' and
__doc__ becomes None.
In Python 2.5+, there's
functools.wraps, which takes care of the
problem of introspection on decorated functions by copying attributes
from the wrapped function.
Django already includes
curry, which is roughly the same as
functools.partial, so it's pretty easy to implement
The attached patch implements
django.utils.functional.wraps, updates all Django decorators to use it, and includes tests to verify that the fixing-up works.