logilab-astng #6015 W0631 false positive (Using possibly undefined loop variable) [resolved]
On the following program:
print [ i for i in range(10) ] print [ i for i in range(10) ]
I get the warning:
W: 2: Using possibly undefined loop variable 'i'
pylint 0.15.0, astng 0.17.3, common 0.35.0 Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)]
Looking at the implementation of W0631 (in checkers/variables.py), the first step is to get the list of assignment statements for variable "i" using "node.lookup(name)". When I print the value of this expression for line 2 of the test case above, it contains two assignments: the "for" on line 1 and the "for" on line 2. However, since "i" is used inside the "for" on line 2, the assignment is guaranteed to be from line 2 as well: if the sequenced that is iterated through is empty, the expression "i" would not be evaluated. So I think lookup() from astng is returning the wrong result: if a loop variable is used inside the expression part of a list comprehension / generator expression, it is guaranteed to be defined by that same list comprehension / generator expression.
|appeared in||<not specified>|
|closed by||<not specified>|