Search code examples
pythonpylint

pylint "Undefined variable" in module written in C++/SIP


I export several native C++ classes to Python using SIP. I don't use the resulting maplib_sip.pyd module directly, but rather wrap it in a Python packagepymaplib:

# pymaplib/__init__.py
# Make all of maplib_sip available in pymaplib.
from maplib_sip import *
...

def parse_coordinate(coord_str):
    ...
    # LatLon is a class imported from maplib_sip.
    return LatLon(lat_float, lon_float)

Pylint doesn't recognize that LatLon comes from maplib_sip:

error  pymaplib  parse_coordinate  40  15  Undefined variable 'LatLon'

Unfortunately, the same happens for all the classes from maplib_sip, as well as for most of the code from wxPython (Phoenix) that I use. This effectively makes Pylint worthless for me, as the amount of spurious errors dwarfs the real problems.

additional-builtins doesn't work that well for my problem:

# Both of these don't remove the error:
additional-builtins=maplib_sip.LatLon
additional-builtins=pymaplib.LatLon

# This does remove the error in pymaplib:
additional-builtins=LatLon

# But users of pymaplib still give an error:
#   Module 'pymaplib' has no 'LatLon' member

How do I deal with this? Can I somehow tell pylint that maplib_sip.LatLon actually exists? Even better, can it somehow figure that out itself via introspection (which works in IPython, for example)?

I'd rather not have to disable the undefined variable checks, since that's one of the huge benefits of pylint for me.

Program versions: Pylint 1.2.1, astroid 1.1.1, common 0.61.0, Python 3.3.3 [32 bit] on Windows7


Solution

  • you may want to try the new --ignored-modules option, though I'm not sure it will work in your case, beside if you stop using import * (which would probably be a good idea as pylint probably already told you ;).

    Rather use short import name, eg import maplib_sip as mls, then prefixed name, eg mls.LatLon where desired.

    Notice though that the original problem is worth an issue on pylint tracker (https://bitbucket.org/logilab/pylint/issues) so some investigation will be done to grasp why it doesn't get member of your sip exported module.