[code-quality] PyLint catching comprehension binding leaks (was: Warn on list comprehension variables used outside list comprehensions?)
Claudiu Popa
pcmanticore at gmail.com
Thu Nov 27 11:15:29 CET 2014
On Wed, Nov 26, 2014 at 6:57 PM, Ian Cordasco
<graffatcolmingov at gmail.com> wrote:
> On Wed, Nov 26, 2014 at 10:51 AM, Keith Derrick <keith.derrick at lge.com> wrote:
>> I couldn't find anything in their bug list about this, so thought I'd check.
>>
>> Just ran pylint at the command line with pylint 1.4 on this file
>> (docstrings and import just to suppress miscellaneous complaints)
>>
>>> 1 ''' Test file for pylint '''
>>> 2 from __future__ import print_function
>>> 3
>>> 4 def method1():
>>> 5 ''' Let x bleed from comprehension '''
>>> 6 [x for x in range(3)]
>>> 7 print(x)
>>> 8
>>> 9 def method2():
>>> 10 ''' reuse/hide x from local scope in comprehension '''
>>> 11 y = 10
>>> 12 [y for y in range(3)]
>>> 13 print(y)
>>> 14
>>
>> For python 2.7.6
>>> ************* Module bug
>>> W: 6, 4: Expression "[x for x in range(3)]" is assigned to nothing
>>> (expression-not-assigned)
>>> W: 7,10: Using possibly undefined loop variable 'x'
>>> (undefined-loop-variable)
>>> C: 11, 4: Invalid variable name "y" (invalid-name)
>>> W: 12, 4: Expression "[y for y in range(3)]" is assigned to nothing
>>> (expression-not-assigned)
>> For python 3.4.0
>>> ************* Module bug
>>> W: 6, 4: Expression "[x for x in range(3)]" is assigned to nothing
>>> (expression-not-assigned)
>>> E: 7,10: Undefined variable 'x' (undefined-variable)
>>> C: 11, 4: Invalid variable name "y" (invalid-name)
>>> W: 12, 4: Expression "[y for y in range(3)]" is assigned to nothing
>>> (expression-not-assigned)
>>
>> So, it warns about the first case in py2 and gives an error in py3 which
>> is expected.
>>
>> But it seems to be confused by the second case, flagging "y" as an
>> invalid variable-name at the assignment point for both versions.
>>
>> 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
>>
>
> At this point, I'd recommend repling to the retitled message to catch
> the PyLint maintainer's focus.
We don't warn for this leak right now, but we are planning to add this
in 1.5, under the --py3k checker. We just didn't have enough time to
add this feature for this release. (See
https://bitbucket.org/logilab/pylint/issue/376 for the remaining work
related to the --py3k checker)
More information about the code-quality
mailing list