Definitely seems like something that should be warned about. For python2 it ought to be an error (which can always be turned off) for either code snippet. For python3, I'd say the two line snippet should flag an error - yes, it will fail at run-time, but pydev in eclipse runs pylint and saves me a lot of grief by spotting this type of thing before I ever save the file (though it doesn't catch either of these, presumably because they are valid python). The three line snippet should probably give a warning ("Are you sure you mean this?") The question is, how do we know which version of Python the code under inspection is targeted for, and hence how to treat this case. Some thoughts on that: * Anyone serious enough to be using pyflakes, pylint, or other static analysis tools is also probably using virtualenv. * pyflakes is therefore most likely running under the same python version for which the code is targeted. * code which is meant to run under multiple python versions is probably tested under something like tox - so again, pyflakes is run under the targeted python version (certainly my tox.ini runs it in every target environment) There's a lot of "probably"'s in there, but I don't think they are unreasonable assumptions. So, could pyflakes reasonably assume that the interpreter under which it is running is also the intended target version for the code under inspection? At least for the major version. This could then be overridden by a suitable command line option to specify the target version(s) Also, in cases like this, possibly a "This will behave differently in Python 2 and 3" warning is a worth while alternative. Keith Derrick | Principal Engineer, Connected Platform | Engineering LG Silicon Valley Lab | 5150 Gt America Parkway, Santa Clara, CA 95054 Office: 408.610-5746 | Mobile: 831.383.9567 | LG.com On 11/26/2014 06:57 AM, Skip Montanaro wrote:
That's not the point of this check. The point of this check is that on Python 2, the binding to x in the comprehension bleeds outside of the comprehension scope Got it.
Still, this code:
x = 10 [x for x in range(3)] print(x + 1)
will run differently in Python 2 than Python3, so even if that was a conscious choice by the author, a --py3k flag should cause a message for this code. This is even worse than the simpler
[x for x in range(3)] print(x + 1)
because at least that will raise a NameError when run in Python 3. The three-line construct will still run, though produce different output.
Skip _______________________________________________ code-quality mailing list code-quality@python.org https://mail.python.org/mailman/listinfo/code-quality