带有ProtoBuf编译的Python结构的VS Code PyLint错误E0602(未定义的变量)

Ale*_*lex 3 conventions pylint protocol-buffers visual-studio-code

我使用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
Run Code Online (Sandbox Code Playgroud)

从pyLint引发错误消息E0602:

未定义变量'lbsnPost'pylint(E0602)
lbsnPost:GeneratedProtocolMessageType

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

元类将枚举扩展为具有整数值的一组符号常量。因此,例如,常数addressbook_pb2.Person.WORK的值为2。

我在我的代码中都遇到了类似的错误(可以正常工作)。我怀疑这是我用错误的约定编写的内容,但是仍然可以使用。但是正确的约定是什么?

截图VSCode Pylint错误E0602

该页面似乎在讨论相同的问题,但是没有一种解决方案有效:
在PyDev
使用协议缓冲区时,即使从导入中导入未定义的变量,即使这样做lbsnpost().LATLNG(实例化protobuf消息),我也会得到相同的未定义的变量错误。

Ale*_*lex 5

我解决了我的问题。显然,pylint在protobuf编译的python类中有(曾经有)问题。有一个软件包可以解决此问题。

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

没有错误!