类型错误:

TypeError: wrapper() got an unexpected keyword argument 'id' 

当我试图执行这些装饰功能:使用Python装饰

问题描述:

我收到以下错误类型意想不到的关键字参数类型错误:</p> <pre><code>TypeError: wrapper() got an unexpected keyword argument 'id' </code></pre> <p>当我试图执行这些装饰功能:使用Python装饰

def owner_required(table): 
    def tags_decorator(func): 
     @wraps(func) # this requires an import 
     def wrapper(id): 
      user_profile = (session['username'], session['picture']) 
      # Connect to the database 
      con = connect() 
      Base.metadata.bind = con 
      # Creates a session 
      DBSession = sessionmaker(bind=con) 
      dbsession = DBSession() 
      if table == 'incidents': 
       query = dbsession.query(Incidents). 
        filter_by(case_num=id).first() 
      if table == 'audits': 
       query = dbsession.query(Audits).filter_by(id=id).first() 
      if table == 'actions': 
       query = dbsession.query(Actions).filter_by(id=id).first() 

      creator = int(query.user_id) 
      ses_user = int(session['user_id']) 
      if 'username' not in session or creator != ses_user: 
       flash("Sorry, %s," 
         " you are not authorized to edit this incident." % 
         session['username']) 
       return redirect('/incidents/') 
      else: 
       func() 
     return wrapper 
    return tags_decorator 

def check_if_report_exists(table): 
    def tags_decorator(func): 
     @wraps(func) # this requires an import 
     def wrapper(**kwargs): 
      # Connect to the database 
      con = connect() 
      Base.metadata.bind = con 
      # Creates a session 
      DBSession = sessionmaker(bind=con) 
      dbsession = DBSession() 
      if table == 'incidents': 
       query = dbsession.query(Incidents).filter_by(case_num=id).first() 
      if table == 'audits': 
       query = dbsession.query(Audits).filter_by(id=id).first() 
      if table == 'actions': 
       query = dbsession.query(Actions).filter_by(id=id).first() 
      if query is None: 
       flash("Sorry, %s," 
         " this report does not exists" % 
         session['username']) 
       return redirect('/dashboard/') 
      else: 
       func(**kwargs) 
     return wrapper 
    return tags_decorator 

这里是与功能装饰者:

app.route('/incidents/edit/<int:id>/', methods=['GET', 'POST']) 
@login_required 
@owner_required('incidents') 
@check_if_report_exists('incidents') 
def editIncident(id): 
    some code... 

本质上,路由传递一个整数到函数使用Fla sk调用具有正确信息的页面。我需要使用与装饰器相同的编号来确保登录的用户是为他们编辑页面的用户。

我一直在关注this guide给装饰者,特别是关于传递装饰者的论证部分。

+0

对于装饰者,使它包装(* args,** kwargs)作为siginature。并将参数传递给func(* args,** kwargs),在您的代码中,您只需编写func() –

+0

我认为您可以按照自己的方式堆叠装饰器。最后一个'check_if_report_exists',是返回一个函数的地方,其中'id'给出一个'TypeError'。如果没有所有的代码,我认为这是不可能的。 – Dave

+0

我试着添加* args和** kwargs但收到了同样的错误。我编辑原始帖子以包含其他装饰器的代码,并更好地解释我正在尝试完成的事情。 – JTP709

这是一个愚蠢的错误 - 我很专注于错误的装饰。 @login_required没有传递任何参数。

我解决它通过传递(* ARGS,** kwargs)的包装和功能:

def login_required(session): 
    def tags_decorator(func): 
     @wraps(func) # this requires an import 
     def wrapper(*args, **kwargs): 
      logger.info('Checking if user in logged in.') 
      if 'username' not in session: 
       logger.info('User is not logged in.') 
       return redirect('login') 
      else: 
       logger.info('User is logged in.') 
       return func(*args, **kwargs) 
     return wrapper 
    return tags_decorator 

我犯了另一个错误是不返回FUNC(),所以我接到一个视图错误。我假设这是因为我使用Python 3.