11

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.

wich
  • 16,709
  • 6
  • 47
  • 72

2 Answers2

17

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

John Feminella
  • 303,634
  • 46
  • 339
  • 357
12

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
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Meanwhile "pylint: disable-msg=E0702" has been deprecated (just use "pylint: disable=E0702" instead). – antred Apr 18 '16 at 08:22
  • 8
    And "pylint: disable=E0702" is also no longer the newest. You should instead use "pylint: disable=raising-bad-type" – Robert Sep 10 '18 at 13:56