【问题标题】:Why does pylint not detect missing member functions (E1103) in lists?为什么 pylint 没有检测到列表中缺少的成员函数 (E1103)?
【发布时间】:2013-03-21 19:39:27
【问题描述】:

在特定代码段上运行 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 未找到配置文件,使用默认配置 ************* 模块 pylintcheck E1101:6,0:“机器”实例没有“is_reachable”成员 E1101:13,4:“机器”实例没有“is_reachable”成员

【问题讨论】:

    标签: python pylint


    【解决方案1】:

    Python 是动态类型的,分析工具很难理解可能发生的一切。看起来你已经到达了 pylint 理解的尽头。

    【讨论】:

    • 完全同意。现在,一如既往,这是免费软件,所以欢迎使用补丁 :-)
    【解决方案2】:

    内德是对的。作为记录,当 pylint 试图知道例如里面有什么时。列表,它只考虑定义这个列表的语句,而不是对它的所有访问。这就解释了为什么在您的示例案例中,它可以正确检测到 machines 中的内容,但不能正确检测到 m2m3 中的内容(被视为空)。

    【讨论】:

    • 谢谢;我没有区分定义和访问,这似乎是这种情况下的关键。
    猜你喜欢
    • 2019-03-11
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多