Title: Allow inspection of used decorators on a function
Type: enhancement Stage:
Components: Versions:
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, mitar, ncoghlan, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2010-07-05 01:28 by mitar, last changed 2010-08-22 07:43 by ncoghlan. This issue is now closed.

Messages (6)
msg109283 - (view) Author: Mitar (mitar) * Date: 2010-07-05 01:28
Sometimes it is useful to be able to check which decorators are already applied to a function, especially when you are constructing them dynamically. I am proposing that for all decorators in Python would be suggested that they maintain a list of used decorators on a function. It is most useful when all decorators do this.

It is the same reasoning why `inspect` module is in Python - to be able to inspect Python structures, to have this possibility. If you do not want it then you do can use Python structures as they were given to you. But you have an option to inspect them.

And I would like the same to see from decorators. It is not really so important that all decorators update this. If they would not then they would be hidden from inspection. But if developer wants to use inspection then he would choose such decorators which use this (or change them in this way). If all default Python's decorators would already be such it would be much easier to use them.

This could be implement by changing `wraps` function to pass over also the (for example) `__DECORATORS__` attribute of a function to a wrapped function and add this new decorator to a list, `__DECORATORS__`.

By default in a list ids of decorators would be maintained, but this could be overridden by `id` attribute of `wraps` function so that decorator can represent itself in some special way, for example as a tuple of its id and some significant parameters.
msg114508 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2010-08-21 16:38
I have added people from the nosy list of #9567. What do you think about this idea?
msg114514 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-08-21 17:01

It was easy for wraps to add the __wrapped__ attribute, since it has access to both the wrapping function and the function being wrapped.

But it hasn't got a clue as to the identity of the decorator that is applying the wrapper.

Absent a concrete suggestion for how to actually do this cleanly, I'm inclined to reject the idea as being infeasible given the way decorators are applied.
msg114518 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-08-21 17:10
I concur with Nick.  If you want to develop your __DECORATORS__ idea further, it should be done off-list.  ISTM, this is more of a shot in the dark than a concrete plan to solve known, real problems.
msg114559 - (view) Author: Mitar (mitar) * Date: 2010-08-21 20:21
Here is the concrete code how I imagined that:

So this would be not made in code of `wraps` function, but would be contract for implementers to do it, if they want to be visible (maybe they still do not want that). And all built-in decorators should do that then.
msg114640 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-08-22 07:43
That's a lot of boilerplate for minimal gain. The Django commenter was right - proposals that start with "everybody in the world needs to do X differently from the way they do it now for this to be useful" aren't ever likely to gain much traction.
Date User Action Args
2010-08-22 07:43:35ncoghlansetmessages: + msg114640
2010-08-21 20:21:34mitarsetmessages: + msg114559
2010-08-21 17:10:35rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg114518
2010-08-21 17:01:28ncoghlansetmessages: + msg114514
2010-08-21 16:38:35eric.araujosetnosy: + rhettinger, ncoghlan, eric.araujo, r.david.murray
messages: + msg114508
2010-07-05 01:28:13mitarcreate