带有ProtoBuf编译的Python结构的VS Code PyLint错误E0602(未定义的变量) [英] 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):
检查地理精度是否在阈值内或以下
,如果min_geoaccuracy == lbsnPost.LATLNG:
allowed_geoaccuracies = [lbsnPost.LATLNG]
elif min_geoaccuracy == lbsnPost.PLACE:
allowed_geoaccuracies = [lbsnPost.LATLNG .LATLNG,lbsnPost.PLACE]
elif min_geoaccuracy == lbsnPost.CITY:
allowed_geoaccuracies = [lbsnPost.LATLNG,lbsnPost.PLACE,lbsnPost.CITY]
else:
返回True
#如果post_geoaccuracy在allowed_geoaccuracys中检查发布的地理精度
:
返回True
否则:
return False
抛出错误来自pyLint的消息E0602:
未定义变量'lbsnPost'pylint(E0602)
lbsnPost:GeneratedProtocolMessageType
但是,Google
该页面似乎在讨论相同的问题,但没有一种解决方案有效:
lbsnpost()。LATLNG (实例化protobuf消息),我得到相同的未定义变量错误。
我解决了我的问题。 显然,pylint在protobuf编译的python类中有(不得不?)问题。有一个软件包可用可解决此问题。
- 已安装 pylint-protobuf 软件包(
pip install pylint-protobuf
) - 添加了
python.linting.pylintArgs:[ --load-插件, pylint_protobuf]
到VS Code中的用户设置
没有错误!
有关更多信息,请参见 VS代码整理文档
I was using Visual Studio for a long time, but it was becoming too complicated to maintain. Now I tried to move to VS Code, but it throws a number of PyLint error messages that don't make sense to me (and the program still works as expected). These errors happen primarily with Python code generated from a GoogleProtoBuf structure.
For example:
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
Throws error message E0602 from pyLint:
Undefined variable 'lbsnPost' pylint (E0602)
lbsnPost: GeneratedProtocolMessageType
However, Google explicitly states that this form of type-referencing is correct:
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.
I get similar errors all over my code (that works fine). I suspect that this is something that I have written in the wrong convention, but somehow still works. But what is the right convention?
This page seems to discuss the same issue, but none of the solutions work:
Undefined variable from import when using protocol buffers in PyDev
that is, even when doing lbsnpost().LATLNG
(instantiating the protobuf message), I get the same undefined variable error.
I solved my problem. Apparently, pylint has (had?) problems with protobuf compiled python classes. There's a package available that solves this issue.
- installed pylint-protobuf package (
pip install pylint-protobuf
) - added
"python.linting.pylintArgs": ["--load-plugins", "pylint_protobuf"]
to User Settings in VS Code
No errors!
For more information, see the VS Code linting Docs
这篇关于带有ProtoBuf编译的Python结构的VS Code PyLint错误E0602(未定义的变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!