[Python-projects] Incorrect unused variable warning

Maarten ter Huurne maarten at treewalker.org
Mon Nov 10 00:10:01 CET 2008


On Sunday 09 November 2008, Marc 'BlackJack Rintsch wrote:
> On Sunday 09 November 2008, Andres Riancho wrote:
> > skip,
> >
> > On Sat, Nov 8, 2008 at 12:35 AM,  <skip at pobox.com> wrote:
> > > pylinting this code:
> > >
> > >    def f():
> > >        for a in range(10):
> > >            pass
> > >
> > >        y = set([a**2 for a in range(10)])
> > >
> > > yields this output:
> > >
> > >    pylintex.py:5: [W, f] Using possibly undefined loop variable 'a'
> > >    pylintex.py:5: [W, f] Unused variable 'y'
> > >
> > > If you take away the for loop it doesn't complain about a.
> >
> > It's not an error (IMHO), here is an example of why:
> > >>> for a in []:
> >
> > ...     pass
> > ...
> >
> > >>> a
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > NameError: name 'a' is not defined
>
> But that is not the case in Skip's snippet.  Pylint seems to think the
> `a` *used* in line 5 might be uninitialized.  But it gets a value from
> the iterable in the list comprehension, or is not used at all if that
> iterable is empty.

I openend a ticket about this problem a couple of weeks ago:
  http://www.logilab.org/ticket/6015

I think the problem originates in the astng code that looks for the place 
where a variable is assigned. It would be great if someone more familiar 
with astng could look into this.

Bye,
		Maarten




More information about the Python-Projects mailing list