Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

Screenshot VSCode Pylint Error E0602

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.

like image 891
Alex Avatar asked Dec 25 '18 07:12

Alex


People also ask

Why is VSCode not recognizing Python import?

The message simply means that VSCode cannot detect the correct path for a Python module. The cause of "Unresolved Import" could be one of the following reason: VSCode is using the wrong Python path. This is often the case if you're running your code against a virtual environment.

How do I fix the import error in VSCode?

To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.

How do you change the Python interpreter in Visual Studio code?

To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.defaultInterpreterPath , which is in the Python extension section of User Settings, with the appropriate interpreter.


1 Answers

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

like image 65
Alex Avatar answered Oct 08 '22 22:10

Alex