[Python-projects] Incorrect unused variable warning

Andres Riancho andres.riancho at gmail.com
Sun Nov 9 07:12:08 CET 2008


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
>>>

pylint can't actually know if "range(10)" is really going to yield
something or not (like in []). So, "a" can be undefined (like in my
example). The correct thing would look like:

>>> a = None
>>> for a in []:
...     pass
...
>>> a
>>>


> --
> Skip Montanaro - skip at pobox.com - http://www.webfast.com/~skip/
> _______________________________________________
> Python-Projects mailing list
> Python-Projects at lists.logilab.org
> http://lists.logilab.org/mailman/listinfo/python-projects
>



-- 
Andres Riancho
http://w3af.sourceforge.net/
Web Application Attack and Audit Framework


More information about the Python-Projects mailing list