VS Code PyLint Error E0602 (undefined variable) with ProtoBuf compiled Python 结构

VS Code PyLint Error E0602 (undefined variable) with ProtoBuf compiled Python Structure

我使用 Visual Studio 很长时间了,但它变得太复杂而无法维护。现在我尝试转移到 VS Code,但它抛出了一些对我来说没有意义的 PyLint 错误消息(而且程序仍然按预期工作)。这些错误主要发生在从 GoogleProtoBuf 结构生成的 Python 代码中。

例如:

from lbsnstructure.lbsnstructure_pb2 import lbsnPost

def geoaccuracy_within_threshold(post_geoaccuracy, min_geoaccuracy):
    """Checks if geoaccuracy is within or below threshhold defined"""

    if min_geoaccuracy == lbsnPost.LATLNG:
        allowed_geoaccuracies = [lbsnPost.LATLNG]
    elif min_geoaccuracy == lbsnPost.PLACE:
        allowed_geoaccuracies = [lbsnPost.LATLNG, lbsnPost.PLACE]
    elif min_geoaccuracy == lbsnPost.CITY:
        allowed_geoaccuracies = [lbsnPost.LATLNG, lbsnPost.PLACE, lbsnPost.CITY]
    else:
        return True
    # check post geoaccuracy
    if post_geoaccuracy in allowed_geoaccuracies:
        return True
    else:
        return False

从 pyLint 中抛出错误消息 E0602:

Undefined variable 'lbsnPost' pylint (E0602)
lbsnPost: GeneratedProtocolMessageType

但是,Google explicitly states 这种形式的类型引用是正确的:

Enums are expanded by the metaclass into a set of symbolic constants with integer values. So, for example, the constant addressbook_pb2.Person.WORK has the value 2.

我的代码中出现了类似的错误(运行正常)。我怀疑这是我在错误的约定中写的东西,但不知何故仍然有效。但什么是正确的约定?

此页面似乎讨论了相同的问题,但 none 的解决方案有效:
Undefined variable from import when using protocol buffers in PyDev
也就是说,即使在执行 lbsnpost().LATLNG(实例化 protobuf 消息)时,我也会收到相同的未定义变量错误。

我解决了我的问题。 Apparently, pylint has (had?) problems with protobuf compiled python classes. There's a package available 解决了这个问题。

  • 已安装 pylint-protobuf 软件包 (pip install pylint-protobuf)
  • 已将 "python.linting.pylintArgs": ["--load-plugins", "pylint_protobuf"] 添加到 VS Code 中的用户设置

没有错误!

有关详细信息,请参阅 VS Code linting Docs

检查 .vscode 文件夹中的 settings.json 我在 virtualenv 工作,但源 python 路径是本地路径。将 python 的路径更改为 virtualenv。 为我工作。 要知道在激活 VE 的终端中写入 "which python3" 的路径。