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

跟風遠走 提交于 2019-12-07 16:31:15

问题


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.


回答1:


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-plugin","pylint_protobuf"] to User Settings in VS Code

No errors!




回答2:


Check settings.json in .vscode folder I work in virtualenv but the source python path is local path. Change python's path to virtualenv. work to me. To know the path write "which python3" in the terminal with the VE activated.



来源:https://stackoverflow.com/questions/53920311/vs-code-pylint-error-e0602-undefined-variable-with-protobuf-compiled-python-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!