Fix naive introspection when using Django decorators
|Reported by:||jdunck||Owned by:||gwilson|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Description (last modified by gwilson)
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'
and __doc__ of 'Adds stuff'.
After decorating, 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 functools.wraps.
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.
Change History (13)
Changed 8 years ago by jdunck
comment:2 Changed 8 years ago by gwilson
- Description modified (diff)
- Triage Stage changed from Unreviewed to Accepted