Opened 17 years ago

Last modified 16 years ago

#5701 closed

Fix naive introspection when using Django decorators — at Version 1

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 Jeremy Dunck)

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

by Jeremy Dunck, 17 years ago

patch against 6454

comment:1 by Jeremy Dunck, 17 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top