How can I convince pylint that my list is iterable?

Viewed 174

I've got a Python application and because of a third-party library I have to use Python 2.7, but I'm trying to make the best of it.

I've downloaded PyLint 1.9.5 and I'm basically getting it to work. One thing I'm stumbling over and can't find a solution to.

I've got some classes basically like this:

class Narg(object):
   [snip]

class Foo(object):
   def __init__(self):
      self.bar = None   # type: Optional[list[Narg]]

   def find(self):
      if self.bar:
         for narg in self.bar:
            [snip]

pylint doesn't like this, it gives me

E:201,80: Non-iterable value self.bar is used in an iterating context (not-an-iterable)

The code runs fine, so it's actually good, how can I convince pylint of this?

1 Answers

pylint has no way of knowing that self.bar might later be updated from being None to being an iterable. You therefore have 2 choices:

  1. Instantiate self.bar as an empty iterable - e.g. self.bar = []
  2. disable pylint: self.bar = None # pylint: disable=not-an-iterable
Related