Search code examples
pythonflaskdecoratorpython-decorators

How to group decorators in Python


In Flask I'm using a set of decorators for each route, but the code is "ugly":

@app.route("/first")
@auth.login_required
@crossdomain(origin='*')
@nocache
def first_page:
    ....

@app.route("/second")
@auth.login_required
@crossdomain(origin='*')
@nocache
def second_page:
    ....

I would prefer to have a declaration that groups all of them with a single decorator:

@nice_decorator("/first")
def first_page:
    ....

@nice_decorator("/second")
def second_page:
    ....

I tried to follow the answer at Can I combine two decorators into a single one in Python? but I cannot make it working:

def composed(*decs):
    def deco(f):
        for dec in reversed(decs):
            f = dec(f)
        return f
    return deco

def nice_decorator(route):
    composed(app.route(route),
             auth.login_required,
             crossdomain(origin="*"),
             nocache)

@nice_decorator("/first")
def first_page:
    ....

because of this error that I don't understand:

@nice_decorator("/first")
TypeError: 'NoneType' object is not callable

Following one of the comments I defined it with another form that works but without the possibility to specify the route parameter:

new_decorator2 = composed(app.route("/first"),
                          auth.login_required,
                          crossdomain(origin="*"),
                          nocache)

Is it possible to define a single decorator with parameters?


Solution

  • You're not defining the composition correctly. You need to change the definition of nice_decorator to something like this:

    def nice_decorator(route):
        return composed(
            app.route(route),
            auth.login_required,
            crossdomain(origin="*"),
            nocache
        )
    

    Your previous version never actually returned anything. Python isn't like Ruby or Lisp where the last expression is the return value.