Warn about string exceptions

Bug #848470 reported by Tristan Seligmann
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Pyflakes
New
Wishlist
Unassigned
pyflakes (Debian)
Confirmed
Unknown

Bug Description

These can take several forms:

   raise "shouldn't happen"
   raise 'Cannot determine type of callable: '+repr(callableObject)
   raise "Can't read input parameters file : %s" % options.input_file

Some of these will be harder to detect than others.

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

Pyflakes still supports python 2.5, where string exceptions are still allowed. Pyflakes has no way to know what the target python version is, so must take the conservative approach and assume the code might be targeted at python 2.5 and string exceptions are valid.

For code targeted at later versions, I suggest catching these errors in unit tests.

Revision history for this message
Tristan Seligmann (mithrandi) wrote :

Unfortunately the most common occurrences of string exceptions are in exceptional code paths that are never (and perhaps have never) been tested; but perhaps pyflakes is not the right tool for dealing with this problem (although, since pyflakes already has to deal with Python 2 vs Python 3, I could imagine support being added for more precise version targets).

Revision history for this message
Florent (florent.x) wrote :

According to the Python documentation:

« Raising of strings as exceptions, as in the statement raise "Error occurred", is deprecated in Python 2.5 and will trigger a warning. The aim is to be able to remove the string-exception feature in a few releases. »
https://docs.python.org/2.7/whatsnew/2.5.html#pep-352-exceptions-as-new-style-classes

So we could probably enforce this deprecation in a linter tool which supports Python 2.5.

With the examples posted in the initial requests, I think that this checker will be easy to implement for "simple cases" as a warning in the "pep8.py" tool, which does non-AST analysis of the code.

Revision history for this message
Adi Roiban (adiroiban) wrote :

+1 for fixing this in pep8.py

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

Other bug subscribers

Remote bug watches

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