Google Groups Home
Help | Sign in
Decoration and aliasing
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Jeremy Dunck  
View profile
 More options Oct 4 2007, 4:15 pm
From: "Jeremy Dunck" <jdu...@gmail.com>
Date: Thu, 4 Oct 2007 15:15:53 -0500
Local: Thurs, Oct 4 2007 4:15 pm
Subject: Decoration and aliasing
It's fairly well-known that decorators are useful, but raises 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

Since Django already includes curry, which is roughly the same as
functools.partial, it'd be pretty easy to back-port "wraps" to Django.

Is there any interest in a patch that modifies Django's built-in
decorators to use functools.wraps in order to preserve things like
view function doc strings?

....While I'm at it, I think aliasing functools.partial to
functional.curry would be good, assuming functools is available.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marty Alchin  
View profile
 More options Oct 4 2007, 4:30 pm
From: "Marty Alchin" <gulop...@gamemusic.org>
Date: Thu, 4 Oct 2007 16:30:24 -0400
Local: Thurs, Oct 4 2007 4:30 pm
Subject: Re: Decoration and aliasing

> Since Django already includes curry, which is roughly the same as
> functools.partial, it'd be pretty easy to back-port "wraps" to Django.

> Is there any interest in a patch that modifies Django's built-in
> decorators to use functools.wraps in order to preserve things like
> view function doc strings?

I love functools.wraps, but I never considered back-porting it in pure
Python like curry. I'd personally love to see this happen, and I'd be
glad to help out if you need it.

OT: I hope nobody uses this as an opportunity to complain about the
naming of "curry". There was some discussion pre-2.5 about the
difference between functools.partial and a true curry, but I don't
think it's not worth making that distinction now.

-Gul


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile
 More options Oct 4 2007, 4:32 pm
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Thu, 04 Oct 2007 16:32:36 -0400
Local: Thurs, Oct 4 2007 4:32 pm
Subject: Re: Decoration and aliasing
On Thu, 2007-10-04 at 15:15 -0500, Jeremy Dunck wrote:

[...]

> Is there any interest in a patch that modifies Django's built-in
> decorators to use functools.wraps in order to preserve things like
> view function doc strings?

Definitely interest from me. It's a bit of a bug at the moment that we
lose docstrings when we decorate things. Particular admin-docs loses
stuff.

So, yeah, go for it.

> ....While I'm at it, I think aliasing functools.partial to
> functional.curry would be good, assuming functools is available.

Providing the functionality is identical. Otherwise it's really, really
painful to work out why things are behaving differently. We don't appear
to be dramatically missing anything in our current currying stuff, so if
this change would introduce different behaviour between 2.3 and, say,
2.5, I'd be against it.

Regards,
Malcolm


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marty Alchin  
View profile
 More options Oct 4 2007, 7:13 pm
From: "Marty Alchin" <gulop...@gamemusic.org>
Date: Thu, 4 Oct 2007 19:13:37 -0400
Local: Thurs, Oct 4 2007 7:13 pm
Subject: Re: Decoration and aliasing
On 10/4/07, Malcolm Tredinnick <malc...@pointy-stick.com> wrote:

> > ....While I'm at it, I think aliasing functools.partial to
> > functional.curry would be good, assuming functools is available.

> Providing the functionality is identical. Otherwise it's really, really
> painful to work out why things are behaving differently. We don't appear
> to be dramatically missing anything in our current currying stuff, so if
> this change would introduce different behaviour between 2.3 and, say,
> 2.5, I'd be against it.

For some reason, I seem to recall there was some difference in how
partial handles duplicate keyword arguments in the final call, as
opposed to how curry does it. But when I actually do a side-by-side
now, I can't discover any differences. So either I haven't figured out
what I was doing before, or I was just smoking something at the time.

-Gul


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Dunck  
View profile
 More options Oct 4 2007, 7:20 pm
From: "Jeremy Dunck" <jdu...@gmail.com>
Date: Thu, 4 Oct 2007 18:20:32 -0500
Local: Thurs, Oct 4 2007 7:20 pm
Subject: Re: Decoration and aliasing
On 10/4/07, Malcolm Tredinnick <malc...@pointy-stick.com> wrote:

> On Thu, 2007-10-04 at 15:15 -0500, Jeremy Dunck wrote:
> [...]
> > Is there any interest in a patch that modifies Django's built-in
> > decorators to use functools.wraps in order to preserve things like
> > view function doc strings?

> Definitely interest from me. It's a bit of a bug at the moment that we
> lose docstrings when we decorate things. Particular admin-docs loses
> stuff.

functools.wraps keeps these:
WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
WRAPPER_UPDATES = ('__dict__',)

> > ....While I'm at it, I think aliasing functools.partial to
> > functional.curry would be good, assuming functools is available.

> Providing the functionality is identical. Otherwise it's really, really
> painful to work out why things are behaving differently. We don't appear
> to be dramatically missing anything in our current currying stuff, so if
> this change would introduce different behaviour between 2.3 and, say,
> 2.5, I'd be against it.

As far as I'm aware, the semantics are the same.

But on closer inspection,  functools.partial actually returns a new
type rather than a simple function.  Things like inspect.getargspec
fail on the result from functools.partial.  With that in mind, I'll
leave it out.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Dunck  
View profile
 More options Oct 5 2007, 9:28 am
From: "Jeremy Dunck" <jdu...@gmail.com>
Date: Fri, 5 Oct 2007 08:28:21 -0500
Local: Fri, Oct 5 2007 9:28 am
Subject: Re: Decoration and aliasing
On 10/4/07, Jeremy Dunck <jdu...@gmail.com> wrote:

> On 10/4/07, Malcolm Tredinnick <malc...@pointy-stick.com> wrote:

> > On Thu, 2007-10-04 at 15:15 -0500, Jeremy Dunck wrote:
> > [...]
> > > Is there any interest in a patch that modifies Django's built-in
> > > decorators to use functools.wraps in order to preserve things like
> > > view function doc strings?

> > Definitely interest from me. It's a bit of a bug at the moment that we
> > lose docstrings when we decorate things. Particular admin-docs loses
> > stuff.

So, I've found a couple decorators I never knew existed.  :)

Is there a reason django.views.http.require_http_method and friends
aren't documented?

I can add docs and tests...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile
 More options Oct 5 2007, 11:10 am
From: "Jacob Kaplan-Moss" <jacob.kaplanm...@gmail.com>
Date: Fri, 5 Oct 2007 10:10:52 -0500
Local: Fri, Oct 5 2007 11:10 am
Subject: Re: Decoration and aliasing
On 10/5/07, Jeremy Dunck <jdu...@gmail.com> wrote:

> So, I've found a couple decorators I never knew existed.  :)

> Is there a reason django.views.http.require_http_method and friends
> aren't documented?

Ha - I'd forgotten about those myself :)

> I can add docs and tests...

Yes, please!

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Dunck  
View profile
 More options Oct 8 2007, 12:27 am
From: "Jeremy Dunck" <jdu...@gmail.com>
Date: Sun, 7 Oct 2007 23:27:19 -0500
Local: Mon, Oct 8 2007 12:27 am
Subject: Re: Decoration and aliasing
On 10/4/07, Malcolm Tredinnick <malc...@pointy-stick.com> wrote:

> On Thu, 2007-10-04 at 15:15 -0500, Jeremy Dunck wrote:
> [...]
> > Is there any interest in a patch that modifies Django's built-in
> > decorators to use functools.wraps in order to preserve things like
> > view function doc strings?

> Definitely interest from me. It's a bit of a bug at the moment that we
> lose docstrings when we decorate things. Particular admin-docs loses
> stuff.

> So, yeah, go for it.

Patch attached to this ticket:
http://code.djangoproject.com/ticket/5701

I note that this is a (perhaps subtle-enough) backwards-incompatible change.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile
 More options Oct 8 2007, 11:49 am
From: "Jacob Kaplan-Moss" <jacob.kaplanm...@gmail.com>
Date: Mon, 8 Oct 2007 10:49:14 -0500
Local: Mon, Oct 8 2007 11:49 am
Subject: Re: Decoration and aliasing
On 10/7/07, Jeremy Dunck <jdu...@gmail.com> wrote:

> I note that this is a (perhaps subtle-enough) backwards-incompatible change.

Can you say a bit more about how this is backwards-incompatible? I'm
having a dense morning, and can't seem to see any breakage here (which
is a good thing!)

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile
 More options Oct 8 2007, 11:54 am
From: "Jacob Kaplan-Moss" <jacob.kaplanm...@gmail.com>
Date: Mon, 8 Oct 2007 10:54:28 -0500
Local: Mon, Oct 8 2007 11:54 am
Subject: Re: Decoration and aliasing
On 10/7/07, Jeremy Dunck <jdu...@gmail.com> wrote:

Oh yeah, and one other thing:

IANAL, but I think the Python Software License is not compatible with
the BSD, so we'll need permission from the PSF to distribute code
copied from functools.py, and we'll need to include the original
copyright statement.

I notice as I write this that we only sorta followed this approach in
the other bits from Python (_threading_local and _decimal). That
really needs to be fixed. Grumble.

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Forest Bond  
View profile
 More options Oct 8 2007, 11:55 am
From: Forest Bond <for...@alittletooquiet.net>
Date: Mon, 8 Oct 2007 11:55:41 -0400
Local: Mon, Oct 8 2007 11:55 am
Subject: Re: Decoration and aliasing

Hi,

On Mon, Oct 08, 2007 at 10:49:14AM -0500, Jacob Kaplan-Moss wrote:
> On 10/7/07, Jeremy Dunck <jdu...@gmail.com> wrote:
> > I note that this is a (perhaps subtle-enough) backwards-incompatible change.

> Can you say a bit more about how this is backwards-incompatible? I'm
> having a dense morning, and can't seem to see any breakage here (which
> is a good thing!)

I think Jeremy was simply pointing out that object attributes are being
permanently changed and somebody, somewhere may be relying on them currently.
In this particular case that seems unlikely, of course.

-Forest
--
Forest Bond
http://www.alittletooquiet.net

  signature.asc
< 1K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Dunck  
View profile
 More options Oct 8 2007, 12:07 pm
From: "Jeremy Dunck" <jdu...@gmail.com>
Date: Mon, 8 Oct 2007 11:07:38 -0500
Local: Mon, Oct 8 2007 12:07 pm
Subject: Re: Decoration and aliasing
On 10/8/07, Jacob Kaplan-Moss <jacob.kaplanm...@gmail.com> wrote:

> On 10/7/07, Jeremy Dunck <jdu...@gmail.com> wrote:
> > I note that this is a (perhaps subtle-enough) backwards-incompatible change.

> Can you say a bit more about how this is backwards-incompatible? I'm
> having a dense morning, and can't seem to see any breakage here (which
> is a good thing!)

I just mean that current code doing naive introspection is getting
decorator names rather than decorated names, and going forward it'll
get the decorated name, etc.

That's the intent, but it is a change to existing (undocumented) functionality.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Dunck  
View profile
 More options Oct 8 2007, 12:08 pm
From: "Jeremy Dunck" <jdu...@gmail.com>
Date: Mon, 8 Oct 2007 11:08:16 -0500
Local: Mon, Oct 8 2007 12:08 pm
Subject: Re: Decoration and aliasing