Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Pylint give error E0702, raising NoneType, on this raise statement?

Say I have the following code.

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

When I run this code through pylint, I get the following error:

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

Is this a bug in pylint? Is my pylint too old?

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

Note: I know this code doesn't make any sense, it's distilled to its bare bones to expose the issue at hand, normally stuff happens in between line 2 and 3 which could make foobar not be None, and no I can't just raise an exception there, because that happens in another thread that has restrictions on it.

like image 410
wich Avatar asked Feb 09 '10 11:02

wich


2 Answers

It's a known bug. Pylint doesn't do a lot of flow-control inferencing.

like image 92
John Feminella Avatar answered Nov 15 '22 19:11

John Feminella


Luckily you can tell pylint that you know better than it does:

def foo():
    foobar = None
    if foobar is not None:
        raise foobar  # pylint: disable-msg=E0702
like image 39
Ned Batchelder Avatar answered Nov 15 '22 18:11

Ned Batchelder