#30 Should warn about "%d %d %d" % (1, 2)

closed-fixed
nobody
None
5
2005-09-01
2005-08-31
Andrew Bennetts
No

Passing the wrong number of args to string printf-style
formatting should be detected. e.g. this code should
be caught be pychecker:

def f():
"%d %d %d" % (1, 2)

This is a frustrating bug that's all too easy to do.

Discussion

  • Neal Norwitz
    Neal Norwitz
    2005-08-31

    Logged In: YES
    user_id=33168

    In what case doesn't pychecker warn? It's a bug if it
    doesn't warn. This was an early check IIRC. When I use the
    code above, I get:

    tt.py:9: Format string argument count (3) doesn't match
    arguments (2)

     
  • Logged In: YES
    user_id=50945

    Hmm! Here's what I get:

    andrew@trogdor ~ $ cat /tmp/foo.py

    def f():
    "%d %d %d" % (1, 2)

    andrew@trogdor ~ $ pychecker /tmp/foo.py
    Processing foo...

    Warnings...

    /tmp/foo.py:3: Statement appears to have no effect
    andrew@trogdor ~ $ pychecker --version
    0.8.14

    (I have the pychecker packaged with Ubuntu 5.04 (Hoary
    Hedgehog) installed, pychecker 0.8.14-4ubuntu1)

    I wonder why I get a different result?

    Ah, if I use variables rather than constants, I get a
    warning. I guess that's a bug then, but not a very serious one.

    (i.e. "%d %d %d" % (a, b) instead of "%d %d %d" % (1, 2))

    Thanks for the great tool, btw!

     
  • Neal Norwitz
    Neal Norwitz
    2005-09-01

    • status: open --> closed-fixed
     
  • Neal Norwitz
    Neal Norwitz
    2005-09-01

    Logged In: YES
    user_id=33168

    I just tested your exact example above that line with Python
    2.2.x and Python-CVS (2.5) with the current CVS of
    pychecker. I get both warnings--the no effect one and the
    format one. I'm assuming your problem was a bug that's been
    fixed. If you can test with the current CVS version of
    pychecker that would be great. I should have a release out
    in a week or so.

    Ah, I think I know what the problem is/was. Python 2.4
    optimized constant tuples. That problem in pychecker was
    fixed recently.

    Everything is working reasonably well right now (lots of
    bugs were fixed). But I may want to try to sneak a few more
    fixes in if I can resolve any.