当前位置: 代码迷 >> python >> 为什么Pylint在这个加注语句中给出错误E0702,提出NoneType?
  详细解决方案

为什么Pylint在这个加注语句中给出错误E0702,提出NoneType?

热度:66   发布时间:2023-06-16 14:16:41.0

说我有以下代码。

def foo():
    foobar = None
    if foobar is not None:
        raise foobar

当我通过pylint运行此代码时,我收到以下错误:

E0702:4:foo: Raising NoneType while only classes, instances or string are allowed

这是pylint中的错误吗? 我的幽灵太老了吗?

pylint 0.18.0, 
astng 0.19.1, common 0.45.0
Python 2.5.1 (r251:54863, Aug 25 2008, 09:23:26) 

注意:我知道这段代码没有任何意义,它已被提炼到其裸露的骨头以暴露手头的问题,通常情况发生在第2行和第3行之间,这可能使foobar不是None,而且我不能只是在那里引发异常,因为这发生在另一个对它有限制的线程中。

这是 Pylint没有做很多流量控制推理。

幸运的是,你可以告诉pylint你知道的比它更好:

def foo():
    foobar = None
    if foobar is not None:
        raise foobar  # pylint: disable-msg=E0702
  相关解决方案