用C ++ / SIP编写的模块中的pylint“Undefined variable”

时间:2014-05-19 20:07:48

标签: python pylint

我使用SIP将几个本机C ++类导出到Python。我不直接使用生成的maplib_sip.pyd模块,而是将其包装在Python包pymaplib中:

# 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无法识别LatLon来自maplib_sip

error  pymaplib  parse_coordinate  40  15  Undefined variable 'LatLon'

不幸的是,maplib_sip的所有类以及我使用的wxPython(Phoenix)的大多数代码都会发生同样的情况。这有效地使Pylint对我毫无价值,因为虚假错误的数量使真正的问题相形见绌。

additional-builtins对我的问题效果不佳:

# 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

我该如何处理?我能以某种方式告诉pylint maplib_sip.LatLon实际存在吗?更好的是,它能以某种方式通过内省(例如在IPython中工作)来解决这个问题吗?

我宁愿不必禁用未定义​​的变量检查,因为这对我来说是pylint的巨大好处之一。

程序版本: Pylint 1.2.1astroid 1.1.1, common 0.61.0Python 3.3.3 [32 bit] on Windows7

1 个答案:

答案 0 :(得分:0)

您可能想尝试使用新的--ignored-modules选项,但我不确定它是否会适用于您的情况,除非您停止使用import *(这可能是一个好主意因为pylint可能已经告诉过你了;)。

而是使用简短的导入名称,例如import maplib_sip as mls,然后使用前缀名称,例如mls.LatLon

请注意,原始问题值得在pylint跟踪器(https://bitbucket.org/logilab/pylint/issues)上出现问题,因此需要进行一些调查以了解它为什么不能获得sip导出模块的成员。