pylint在C++/SIP模块中出现"未定义变量"错误

1 投票
1 回答
777 浏览
提问于 2025-04-18 07:01

我通过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

我该怎么处理这个问题呢?我能不能告诉Pylintmaplib_sip.LatLon实际上是存在的?更好的是,它能不能通过某种方式自己发现这个(比如在IPython中可以做到)?

我不想禁用未定义变量的检查,因为这正是Pylint对我来说的一个重要好处。

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

1 个回答

0

你可以试试新的 --ignored-modules 选项,不过我不确定这对你是否有效。另外,如果你停止使用 import *(这可能是个好主意,因为pylint可能已经告诉过你了;)

建议使用简短的导入名称,比如 import maplib_sip as mls,然后在需要的地方用前缀名称,比如 mls.LatLon

不过要注意,原来的问题值得在pylint的跟踪器上提个问题(https://bitbucket.org/logilab/pylint/issues),这样可以调查一下为什么你的sip导出模块没有成员。

撰写回答