如何解决装饰器中缺少参数的问题?

问题描述 投票:0回答:1

我得到这个错误,从我 假设 是来自我的装饰者。

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

这是我的装饰器

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

我正在对这段代码进行测试,但我不知道如何去调试这个问题,为什么它可能会缺少封装参数?

python-3.x flask python-decorators
1个回答
0
投票

functools.wraps 需要一个位置参数。由于你没有提供它,它给你一个错误。你需要这样做。

def authenticate_restful(f):
    @wraps(f)
    ...
© www.soinside.com 2019 - 2024. All rights reserved.