Code like this:

    import os
    import tempfile

    def func():
        (fd, tmpfile) = tempfile.mkstemp(dir="/tmp")
        os.close(fd)
        print "{tmpfile}".format(**locals())

generates an unused variable warning for tmpfile, even though it's
referenced in the string in the print statement. The string module has
a Formatter class (since 2.6, apparently) which knows how to tear such
format strings apart:

    >>> for elt in fmt.parse("{} {tmpfile} {1} {0:.3f}"):
    ...   print elt
    ...
    ('', '', '', None)
    (' ', 'tmpfile', '', None)
    (' ', '1', '', None)
    (' ', '0', '.3f', None)

I'm only now getting comfortable with the new string formatting stuff,
but it seems to me that the most common use case will be to call the
format method of a string literal (so this sort of usage should be
fairly easy to detect).  I don't think it should be terribly difficult
to intersect the names coming out of Formatter.parse with the
otherwise unused local variables, but I am completely unfamiliar with
pylint's node traversal handlers (all the visit_* methods in
variables.py).  Can someone point me to some documentation for how
this works, and what visit_* methods I can define?

Thanks,

Skip Montanaro
_______________________________________________
code-quality mailing list
code-quality@python.org
https://mail.python.org/mailman/listinfo/code-quality

Reply via email to