[Python-projects] Incorrect unused variable warning

Marc 'BlackJack Rintsch marc at rintsch.de
Sun Nov 9 08:01:02 CET 2008


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 see no way for a `NameError` in the snippet.  Well, except if 
`range()` is a function that removes the names `set` or `range` from 
`globals()` when called the first time.  ;-)

Ciao,
	Marc 'BlackJack' Rintsch
-- 
Person countSheep := method(
    sheep := 0
    while(self isAsleep isNil, sheep = sheep + 1; yield)
    sheep
)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.logilab.org/pipermail/python-projects/attachments/20081109/86c60d64/attachment.pgp 


More information about the Python-Projects mailing list