PyLint错误地认为一个对象缺少某些属性
在我的代码中,我使用了来自编译扩展的对象(在我的例子中是igraph)。我用PyLint来分析代码。PyLint对缺少属性(比如igraph的Graph.adjacent
)发出了警告,但这些属性显然是存在的(代码运行没有错误)。这可能是什么原因呢?
这里有一些测试代码:
import igraph
gr = igraph.Graph(10)#create a graph with 10 vertices
edges = gr.es #no pylint errors
vertices = gr.vs #no pylint errors
print gr.are_connected(0, 1) #pylint error E1101
print gr.adjacent(0) #pylint error E1101
这是PyLint的输出:
************* Module temp
C0111: 1: Missing docstring
C0103: 2: Invalid name "gr" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C0103: 3: Invalid name "edges" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C0103: 4: Invalid name "vertices" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
E1101: 5: Instance of 'Graph' has no 'are_connected' member
E1101: 6: Instance of 'Graph' has no 'adjacent' member
附注:igraph确实在我的PYTHONPATH中。
1 个回答
1
如果这是一个编译好的C扩展,Pylint就没什么办法了,因为它无法分析源代码。你能在交互式命令行中打印出 igraph.Graph.are_connected 吗?如果不能,那可能是这个库在初始化的时候做了一些奇怪的事情,或者这些方法是被动态检查的。
总之,这对Pylint来说是个棘手的问题。
你可以使用在 http://www.logilab.org/ticket/73978 上提供的补丁(最近已经包含在开发版本中),或者用内联指令来忽略E1101的警告。