Commits

Tim Hatch committed b42533d

Adds check for boolop being used as exception class. Closes #100707.
---
.../third_party/py/pylint/checkers/exceptions.py | 7 +++++++
.../third_party/py/pylint/test/input/func_w0711.py | 15 +++++++++++++++
.../py/pylint/test/messages/func_w0711.txt | 4 ++++
3 files changed, 26 insertions(+), 0 deletions(-)
create mode 100644 /test/input/func_w0711.py
create mode 100644 /test/messages/func_w0711.txt

Comments (0)

Files changed (4)

 ChangeLog for PyLint
 ====================
 
+--
+    * #100707: check for boolop being used as exception class, introducing 
+      new W0711 message (patch by Tim Hatch)
+
+
 2012-07-17  --  0.25.2
     * #93591: Correctly emit warnings about clobbered variable names when an
       except handler contains a tuple of names instead of a single name.

checkers/exceptions.py

     'W0710': ('Exception doesn\'t inherit from standard "Exception" class',
               'Used when a custom exception class is raised but doesn\'t \
               inherit from the builtin "Exception" class.'),
+    'W0711': ('Exception to catch is the result of a binary "%s" operation',
+              'Used when the exception to catch is of the form \
+              "except A or B:".  If intending to catch multiple, \
+              rewrite as "except (A, B):"'),
     }
 
 
                 elif index < (nb_handlers - 1):
                     msg = 'empty except clause should always appear last'
                     self.add_message('E0701', node=node, args=msg)
+
+            elif isinstance(handler.type, astng.BoolOp):
+                self.add_message('W0711', node=handler, args=handler.type.op)
             else:
                 try:
                     excs = list(unpack_infer(handler.type))

test/input/func_w0711.py

+"""find binary operations used as exceptions
+"""
+
+__revision__ = 1
+
+try:
+    __revision__ += 1
+except Exception or StandardError:
+    print "caught1"
+except Exception and StandardError:
+    print "caught2"
+except (Exception or StandardError):
+    print "caught3"
+except (Exception or StandardError), exc:
+    print "caught4"

test/messages/func_w0711.txt

+W:  8: Exception to catch is the result of a binary "or" operation
+W: 10: Exception to catch is the result of a binary "and" operation
+W: 12: Exception to catch is the result of a binary "or" operation
+W: 14: Exception to catch is the result of a binary "or" operation