Why does pylint flag this?

We've recently started to see pylint flag issues on a piece of code that looks similar to this - --------------------------------------- #!/bin/python """ Test the TemporaryFile iterator """ import os import tempfile with tempfile.TemporaryFile() as nullfh: nullfh.write(b'Hello world!') nullfh.seek(0, os.SEEK_SET) for line in nullfh: line = line.strip() print line --------------------------------------- With pylint 1.7.1, astroid 1.5.2 Python 2.7.5 (default, Nov 6 2016, 00:28:07) This reports - E: 13,16: Non-iterable value nullfh is used in an iterating context (not-an-iterable) However the code is functional so nullfh does work in the iterating context. Any clues as to why this is being flagged would be appreciated. Thanks, Mark.

Mark Syms <Mark.Syms@citrix.com> writes:
E: 13,16: Non-iterable value nullfh is used in an iterating context (not-an-iterable)
Bear in mind that PyLint performs static analysis; it doesn't execute the code, and so does not have access to the Python objects that running statements produce. Since Python objects are dynamically typed, a static analysis cannot be certain of the type of any object.
However the code is functional so nullfh does work in the iterating context.
I expect PyLint makes an incorrect guess about the type of the return value from ‘tempfile.TemporaryFile()’. -- \ “You can never entirely stop being what you once were. That's | `\ why it's important to be the right person today, and not put it | _o__) off until tomorrow.” —Larry Wall | Ben Finney
participants (2)
-
Ben Finney
-
Mark Syms