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

wic*_*ich 9 python exception pylint

说我有以下代码.

def foo():
    foobar = None
    if foobar is not None:
        raise foobar
Run Code Online (Sandbox Code Playgroud)

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

E0702:4:foo: Raising NoneType while only classes, instances or string are allowed
Run Code Online (Sandbox Code Playgroud)

这是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) 
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*lla 15

这是一个已知的错误.Pylint没有做很多流量控制推理.


Ned*_*der 9

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

def foo():
    foobar = None
    if foobar is not None:
        raise foobar  # pylint: disable-msg=E0702
Run Code Online (Sandbox Code Playgroud)

  • 并且“ pylint:disable = E0702”也不再是最新的。您应该改用“ pylint:disable = raising-bad-type” (2认同)