Search code examples
python-3.xflaskpython-decorators

How to fix missing argument in decorator?


I'm getting this error from what I assume is coming from my decorator:

TypeError: update_wrapper() missing 1 required positional argument: 'wrapper'

this is my decorator:

def authenticate_restful(f):
    @wraps
    def decorated_function(*args, **kwargs):
        response_object = {
            'status': 'fail',
            'message': 'Provide a valid auth token.'
        }
        auth_header = request.headers.get('Authorization')
        if not auth_header:
            return jsonify(response_object), 403
        auth_token = auth_header.split(' ')[1]
        resp = User.decode_auth_token(auth_token)
        if isinstance(resp, str):
            response_object['message'] = resp
            return jsonify(response_object), 401
        user = User.query.filter_by(id=resp['sub']).first()
        if not user or not user.active:
            return jsonify(response_object), 401
        return f(resp, *args, **kwargs)
    return decorated_function

I'm running tests on this block of code and I have no idea how to go about debugging this. Why it can possibly be missing the wrapper argument ?


Solution

  • functools.wraps expects a positional argument. Since you didn't provide it, it gives you an error. You need to do this:

    def authenticate_restful(f):
        @wraps(f)
        ...