2013-03-21 66 views
1

當在特定的代碼段上運行pylint時,如果變量已被添加到帶有.append()或+ = [var]的列表中,我會爲缺失的函數得到錯誤的否定結果。有沒有辦法避免讓pylint在這裏失去變量類型? (pylint的0.27.0,蟒蛇2.7.2)爲什麼pylint沒有在列表中檢測到缺少成員函數(E1103)?

#!/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 

回答

1

內德是對的。爲了記錄,當pylint試圖知道例如列表中,它只考慮定義此列表的陳述,並非全部對其進行訪問。這就解釋了爲什麼在你的示例中,它能夠正確檢測machines中的內容,但不是m2m3(視爲空)。

+0

謝謝;我沒有區分定義和訪問,這似乎是這種情況下的關鍵。 – 2013-03-22 16:09:17

3

Python是動態類型和分析工具也很難理解可能發生的一切。看起來你已經達到了pylint理解的結果。

+0

完全同意。現在,一如既往,這是自由軟件,所以補丁歡迎:-) – 2013-03-22 08:48:49

相關問題