Detect invalid string literal formatting
Bug #1361849 reported by
asmeurer
This bug report is a duplicate of:
Bug #950369: Detect broken string templates that will raise ValueError when interpolated.
Edit
Remove
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Pyflakes |
Confirmed
|
Wishlist
|
Unassigned |
Bug Description
It would be nice if pyflakes could detect invalid formatting errors on string literals. Something like
'abc' % stuff
'%s' % (a, b)
'{}'.format(1, 2)
'{x} {y}'.format(x=1, y=2, z=3)
It would only operate on string literals, so there's no type inferencing or anything.
Changed in pyflakes: | |
importance: | Undecided → Wishlist |
To post a comment you must log in.
So while the first two are certainly wrong, the last one can be difficult realistically.
>>> '{}'.format(1, 2)
'1'
>>> '{a} {b}'.format(a=1, b=2, c=3)
'1 2'
It doesn't balk at the fact that you provided more parameters than necessary. (Not say like '%s' % (1, 2).)
This can also be tricky because you could build up a dictionary and then do '{a} {b}'.format( **my_dict) .
I definitely agree that the simpler cases (using %) should be somewhat easy to handle. I'm not sure about the latter two since str.format seems to allow more arguments than necessary.