diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2011-09-07 10:25:17 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2011-09-07 10:25:17 +0200 |
commit | 9e9fd6cafec51a03c292e6246e802f749b1589b7 (patch) | |
tree | a288336cbd83e292ef35c953f4483f4ce3401a90 | |
parent | 2d0d8ba50abb3dfb9e4a85c544d6daa21fe60936 (diff) | |
download | pylint-9e9fd6cafec51a03c292e6246e802f749b1589b7.tar.gz |
closes #74745: make 'too general' exception names configurable (patch by Google)
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | checkers/exceptions.py | 21 | ||||
-rw-r--r-- | test/messages/func_w0703.txt | 2 |
3 files changed, 19 insertions, 7 deletions
@@ -1,5 +1,6 @@ ChangeLog for PyLint ==================== + -- * #74742: make allowed name for first argument of class method configurable (patch by Google) @@ -8,6 +9,8 @@ ChangeLog for PyLint some cases of "TypeError: '_Yes' object does not support indexing" (patch by Google) + * #74745: make "too general" exception names configurable (patch by Google) + 2011-07-18 -- 0.24.0 * #69738: add regular expressions support for "generated-members" diff --git a/checkers/exceptions.py b/checkers/exceptions.py index 0ba93f8..bf81438 100644 --- a/checkers/exceptions.py +++ b/checkers/exceptions.py @@ -26,6 +26,8 @@ from pylint.checkers.utils import is_empty, is_raising from pylint.interfaces import IASTNGChecker +OVERGENERAL_EXCEPTIONS = ('Exception',) + MSGS = { 'E0701': ( 'Bad except clauses order (%s)', @@ -47,8 +49,9 @@ MSGS = { 'W0702': ('No exception type(s) specified', 'Used when an except clause doesn\'t specify exceptions type to \ catch.'), - 'W0703': ('Catch "Exception"', - 'Used when an except catches Exception instances.'), + 'W0703': ('Catching too general exception %s', + 'Used when an except catches a too general exception, \ + possibly burying unrelated errors.'), 'W0704': ('Except doesn\'t do anything', 'Used when an except clause does nothing but "pass" and there is\ no "else" clause.'), @@ -74,7 +77,14 @@ class ExceptionsChecker(BaseChecker): name = 'exceptions' msgs = MSGS priority = -4 - options = () + options = (('overgeneral-exceptions', + {'default' : OVERGENERAL_EXCEPTIONS, + 'type' :'csv', 'metavar' : '<comma-separated class names>', + 'help' : 'Exceptions that will emit a warning ' + 'when being caught. Defaults to "%s"' % ( + ', '.join(OVERGENERAL_EXCEPTIONS),)} + ), + ) def visit_raise(self, node): """visit raise possibly inferring value""" @@ -163,10 +173,10 @@ class ExceptionsChecker(BaseChecker): msg = '%s is an ancestor class of %s' % ( previous_exc.name, exc.name) self.add_message('E0701', node=handler.type, args=msg) - if (exc.name == 'Exception' + if (exc.name in self.config.overgeneral_exceptions and exc.root().name == EXCEPTIONS_MODULE and nb_handlers == 1 and not is_raising(handler.body)): - self.add_message('W0703', node=handler.type) + self.add_message('W0703', args=exc.name, node=handler.type) exceptions_classes += excs @@ -185,4 +195,3 @@ def inherit_from_std_ex(node): def register(linter): """required method to auto register this checker""" linter.register_checker(ExceptionsChecker(linter)) - diff --git a/test/messages/func_w0703.txt b/test/messages/func_w0703.txt index e37197a..6135664 100644 --- a/test/messages/func_w0703.txt +++ b/test/messages/func_w0703.txt @@ -1 +1 @@ -W: 8: Catch "Exception" +W: 8: Catching too general exception Exception |