19

I want different functions to be executable only if the logged in user has the required permission level.

To make my life more simple I want to use decorators. Below I attempt to set attribute permission on 'decorated' functions - as shown below.

def permission(permission_required):
    def wrapper(func):
        def inner(*args, **kwargs):
            setattr(func, 'permission_required', permission_required)
            return func(*args, **kwargs)
        return inner
    return wrapper

@permission('user')
def do_x(arg1, arg2):

    ...

@permission('admin')
def do_y(arg1, arg2):
    ...

But when I do:

fn = do_x
if logged_in_user.access_level == fn.permission_required:
    ...

I get an error 'function' object has no attribute 'permission_required'

What am I missing?

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Rich Tier
  • 9,021
  • 10
  • 48
  • 71
  • 1
    As a side note: I'm pretty sure you want to use [`functools.wraps`](http://docs.python.org/2/library/functools.html#functools.wraps) here. Not to directly solve your problem, but because it's next to impossible to debug this kind of code when every functions ends up named `inner`, taking `(*args, **kwargs)`, `inspect`ing to the wrong source, etc. – abarnert Apr 03 '13 at 18:18

3 Answers3

28

You are checking the attribute on the inner (wrapper) function, but set it on the original (wrapped) function. But you need a wrapper function at all:

def permission(permission_required):
    def decorator(func):
        func.permission_required = permission_required
        return func
    return decorator

Your decorator needs to return something that'll replace the original function. The original function itself (with the attribute added) will do fine for that, because all you wanted to do is add an attribute to it.

If you still need a wrapper, then set the attribute on the wrapper function instead:

from functools import wraps

def permission(permission_required):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # only use a wrapper if you need extra code to be run here
            return func(*args, **kwargs)
        wrapper.permission_required = permission_required
        return wrapper
    return decorator

After all, you are replacing the wrapped function with the wrapper returned by the decorator, so that's the object you'll be looking for the attribute on.

I also added the @functools.wraps() decorator to the wrapper, which copied across important identifying information and other helpful things from func to the wrapper, making it much easier to work with.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I need the wrapper to allow for arguments in the decorated function. I used your code and it worked great - but when I added wrapper for arguments then the error came back. – Rich Tier Apr 03 '13 at 17:59
  • 1
    @rikAtee: You don't need a wrapper to allow for arguments in the decorated function. The first example just modifies and returns the function; it still takes the exact same arguments it did before being decorated. – abarnert Apr 03 '13 at 18:14
  • @rikAtee: Indeed, the wrapper is *not* required if all you do is set the attribute. Only add a wrapper if there is a need for a wrapper (e.g. adds extra code to operate on the arguments or the return values, or do extra things when the function is called). – Martijn Pieters Apr 03 '13 at 18:16
  • 1
    Don't forget to decorate `wrapper` with `@wraps` (in `functools`), so that the decorated function will have its attributes kept (including `__doc__`)! – minmaxavg Jan 16 '16 at 14:46
1

Your decorator should return a function that can replace do_x or do_y , not return the execution result of do_x or do_y You can modity you decorate as below:

def permission(permission_required):
    def wrapper(func):
        def inner():
            setattr(func, 'permission_required', permission_required)
            return func
        return inner()
    return wrapper

Of course, you have another brief solution:

def permission(permission_required):
    def wrapper(func):
        setattr(func, 'permission_required', permission_required)
        return func
    return wrapper
Yarkee
  • 9,086
  • 5
  • 28
  • 29
  • 1
    Your inner function does *nothing*, so you are replacing the original with a function that does nothing more than set an attribute on the wrapped function, making it *useless*. – Martijn Pieters Apr 03 '13 at 18:03
1

The problem is that, even though you are setting the desired property to the wrapped function in inner, inner is returning whatever is returned by the decorated function, which usually never is the function itself.

You should just return the very same original function with the attribute added, thus you do not really want to worry about what arguments this original decorated function might take, meaning you can get rid of one of the wrapping levels:

def permission(permission_required):
   def wrapper(func):
       setattr(func, 'permission_required', permission_required)
       return func
   return wrapper

@permission('user')
def do_x(arg1, arg2):
    pass

@permission('admin')
def do_y(arg1, arg2):
    pass

This works just fine:

>>> do_x
<function __main__.do_x(arg1, arg2)>
>>> do_x.permission_required
'user'
>>> do_y.permission_required
'admin'
mosegui
  • 664
  • 6
  • 15