Opened 17 years ago

Last modified 16 years ago

#5701 closed

Fix naive introspection when using Django decorators — at Version 2

Reported by: Jeremy Dunck Owned by: nobody
Component: Uncategorized Version: dev
Severity: Keywords: decorators views
Cc: simon@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gary Wilson)

It's fairly well-known that decorators are useful, but raise some issues.

Example:

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.

http://docs.python.org/lib/module-functools.html

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 (3)

by Jeremy Dunck, 17 years ago

patch against 6454

comment:1 by Jeremy Dunck, 17 years ago

Description: modified (diff)

comment:2 by Gary Wilson, 16 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

fixed description formatting.

Note: See TracTickets for help on using tickets.
Back to Top