10

假设我有以下代码。

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太旧了吗?

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,不,我不能只是在那里引发一个异常,因为这发生在另一个对其有限制的线程中。

4

2 回答 2

16

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

于 2010-02-09T11:57:47.290 回答
11

幸运的是,您可以告诉 pylint 您比它更了解:

def foo():
    foobar = None
    if foobar is not None:
        raise foobar  # pylint: disable-msg=E0702
于 2010-02-09T13:52:11.603 回答