在特定代码段上运行pylint时,如果已将变量添加到带有.append()或+ = [var]的列表中,则会对缺少的函数进行错误否定。有没有办法避免让pylint丢失变量类型? (pylint 0.27.0,python 2.7.2)
#!/usr/bin/python
from foo.lib.machine import Machine
lh = Machine('localhost')
lh.is_reachable() #Pylint catches this
machines = [lh]
m2 = []
m2.append(lh)
m3 = []
m3 += [lh]
for m in machines:
m.is_reachable() #Pylint catches this
for m in m2:
m.is_reachable() #Pylint MISSES this
for m in m3:
m.is_reachable() #Pylint MISSES this
$ pylint -i y -E pylintcheck No config file found, using default configuration ************* Module pylintcheck E1101: 6,0: Instance of 'Machine' has no 'is_reachable' member E1101: 13,4: Instance of 'Machine' has no 'is_reachable' member
答案 0 :(得分:3)
Python是动态类型的,分析工具很难理解可能发生的一切。看起来你已经达到了pylint理解的结束。
答案 1 :(得分:1)
Ned是对的。为了记录,当pylint试图知道例如列表,它只考虑定义此列表的语句,而不是对它的所有访问。这就解释了为什么在您的示例中,它会正确检测到machines
中的内容,而不是m2
或m3
中的内容(被视为空)。