【问题标题】:How to fix missing argument in decorator?如何修复装饰器中缺少的参数?
【发布时间】:2020-09-28 10:29:51
【问题描述】:

假设来自我的装饰器,我收到了这个错误:

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】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2022-06-28
      • 1970-01-01
      • 2019-08-24
      • 2019-11-26
      • 2023-04-03
      • 2022-01-22
      • 1970-01-01
      • 2019-09-07
      相关资源
      最近更新 更多