[TIP] Teaching pylint about keyword placeholders in string formatting

Ben Finney ben+python at benfinney.id.au
Wed Jul 15 03:08:13 PDT 2009


Howdy all,

A common idiom I use is::

    def frobnicate(warble):
        foo = complex_computation()
        bar = long.access.path.leading.to.useful.value
        baz = (lengthy + expression * with_several_parts)
        spangulate("%(warble)s: %(foo)s%(bar)s [%(baz)d]" % vars())

This allows the format of the string to be clear, and allows the
separate parts of it to have meaningful names.

However, this is causing pylint to complain::

    W:218:frobnicate: Unused argument 'warble'
    W:219:frobnicate: Unused variable 'foo'
    W:220:frobnicate: Unused variable 'bar'
    W:221:frobnicate: Unused variable 'baz'

That is, pylint is not aware that the names used by accessing the values
from the dictionary returned by ‘vars()’.

This warning, when it actually *does* detect unused name bindings, is
very useful; I don't want to disable it. Nor do I want to pepper my code
with hints to pylint about each one of these, detracting significantly
from the readability which is the main point of the above idiom.

How can I make pylint aware in the general case that the above idiom
does, in fact, constitute a use of the names ‘warble’, ‘foo’, ‘bar’, and
‘baz’ in the code?

-- 
 \         “Pinky, are you pondering what I'm pondering?” “I think so, |
  `\    Brain, but if we give peas a chance, won't the lima beans feel |
_o__)                                left out?” —_Pinky and The Brain_ |
Ben Finney




More information about the testing-in-python mailing list