summaryrefslogtreecommitdiff
path: root/pylint/checkers/exceptions.py
diff options
context:
space:
mode:
authorSteven Myint <hg@stevenmyint.com>2015-03-13 06:51:47 -0700
committerSteven Myint <hg@stevenmyint.com>2015-03-13 06:51:47 -0700
commitb55a26da5afe8b5cbd40c45835a9aa4920012b59 (patch)
treeba1f141be3bb66167c9017acbe750c389f4910ae /pylint/checkers/exceptions.py
parentdb4bd177da1a549359f97f708975a797a613cfae (diff)
downloadpylint-b55a26da5afe8b5cbd40c45835a9aa4920012b59.tar.gz
Add "duplicate-except" checker
It checks if an exception type was already caught previously in the same try/except. This fixes #485.
Diffstat (limited to 'pylint/checkers/exceptions.py')
-rw-r--r--pylint/checkers/exceptions.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index 88a8f22..835ad4a 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -14,6 +14,7 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""exceptions handling (raising, catching, exceptions classes) checker
"""
+import collections
import sys
import astroid
@@ -95,6 +96,10 @@ MSGS = {
'pointless-except',
'Used when an except clause does nothing but "pass" and there is\
no "else" clause.'),
+ 'W0705': ('Catching previously caught exception type %s',
+ 'duplicate-except',
+ 'Used when an except catches a type that was already caught by '
+ 'a previous handler.'),
'W0710': ('Exception doesn\'t inherit from standard "Exception" class',
'nonstandard-exception',
'Used when a custom exception class is raised but doesn\'t \
@@ -324,7 +329,12 @@ class ExceptionsChecker(BaseChecker):
self.add_message('broad-except',
args=exc.name, node=handler.type)
- exceptions_classes += [exc for _, exc in excs]
+ for (_, current_exc) in excs:
+ if current_exc in exceptions_classes:
+ self.add_message('duplicate-except',
+ args=exc.name, node=handler.type)
+
+ exceptions_classes += [exc_ for _, exc_ in excs]
def register(linter):