Flask-“未定义名称'body'”-Visual Studio Code Linter

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

我已经在Windows上使用Python 3.8设置了这个简单的Flask应用。我使用swagger生成API:

下面是从我的一条路径中切出的部分:

#swagger.yaml
paths:
  /nlp/classification/ProfanityClassification:
    post:
      operationId: api.nlp.classification.ProfanityClassification.main
      requestBody:
        description: The input text to check for profanity in.
        content:
          [......]

如您所见,operationIdapi.nlp.classification.ProfanityClassification.main

在我的ProfanityClassification.py文件中,我有这个:

from flask import json, jsonify
class ProfanityClassification:

    def __init__(self, body):
        self.body = body

    def run(self):
        sentence = self.body['text']
        return jsonify({"success": json.dumps(
            True), "message": "Success!"}), 200

def main(body):
    model = ProfanityClassification(body)
    return model.run()

if __name__ == "__main__":
    main(body)

在我的Visual Studio代码中,它用main(body)标记错误Name 'Body' is not defined

但是据我了解,body变量来自Swagger文件。

我该如何解决?

al

python flask visual-studio-code pylint mypy
1个回答
0
投票

要突出显示的变量body并未在代码中的任何地方定义。这就是为什么您的短毛绒指向body is not defined

请确保您定义了一个名为body的变量,并将其传递给main函数。然后,lint将指向新创建的body变量。

© www.soinside.com 2019 - 2024. All rights reserved.