Detect broken string templates that will raise ValueError when interpolated

Bug #950369 reported by Jamu Kakar
20
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Pyflakes
Triaged
Wishlist
Unassigned

Bug Description

For example, code like this will break:

  >>> "%(foo)" % {"foo": "bar"}
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  ValueError: incomplete format

It'd be awesome if Pyflakes told me about that kind of issue.

Jamu Kakar (jkakar)
summary: - It would be nice to detect string templates that will break when
+ Detect broken string templates that will raise ValueError when
interpolated
Revision history for this message
Jamu Kakar (jkakar) wrote :

I poked through the code and determined that implementing this kind of
check will be a pretty big pain in the neck. We'd need to parse the
string to determine what kind of interpolation is required and then
verify that with the arguments to be interpolated. ie, we'd need to
implement our own string interpolation engine. Ew.

Revision history for this message
Jean-Paul Calderone (exarkun) wrote :

A patch to implement this was sent to the mailing list, https://lists.launchpad.net/divmod-dev/msg00193.html

Revision history for this message
Jakub Wilk (jwilk) wrote :

Implementing your own string interpolation engine may sound scary at first, but in fact it should be quite easy. For example, pylint has a template parser that is just ~60 lines of code:
https://bitbucket.org/logilab/pylint/src/36355f1fe1fd/checkers/utils.py?at=default#cl-277

I took a different approach in lintian4python, which resulted in even shorted code:
https://bitbucket.org/jwilk/lintian4python/src/5e552f08d8f5/helpers/python/code-analysis?at=default#cl-292

Revision history for this message
asmeurer (asmeurer) wrote :

Too bad they're both GPL, as you otherwise could literally just reuse that code.

Revision history for this message
Jakub Wilk (jwilk) wrote :

The relevant part of lintian4python is under the same license as pyflakes.

Phil Frost (bitglue)
Changed in pyflakes:
importance: Undecided → Wishlist
status: New → Triaged
Revision history for this message
asmeurer (asmeurer) wrote :

Rather than rewriting Python's string interpolation, would it be possible to just take the string and interpolate it with some dummy objects that have sufficient __int__, __float__, __str__, etc. defined on them, and see if any errors are raised (basically, reuse Python's own engine)?

Revision history for this message
Jakub Wilk (jwilk) wrote :

Absolutely. That's what I did in lintian4python.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.