summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcpopa <devnull@localhost>2014-04-22 23:20:53 +0300
committercpopa <devnull@localhost>2014-04-22 23:20:53 +0300
commit535b6eab04ed6bf40e8c7e1fa37880888254bc6d (patch)
tree0208362126864b19bd7dc11726cfe2dd98bd4dce
parent41ce7ee12f464ba0c2a5967a43e1a1a93f1081c6 (diff)
downloadpylint-535b6eab04ed6bf40e8c7e1fa37880888254bc6d.tar.gz
Add 'indexing-exception' warning, which detects that indexing an exception occurs in Python 2 (behaviour removed in Python 3)
-rw-r--r--ChangeLog3
-rw-r--r--checkers/exceptions.py16
-rw-r--r--test/input/func_indexing_exceptions_py_30.py16
-rw-r--r--test/messages/func_indexing_exceptions_py_30.txt4
4 files changed, 39 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 8bb3ba9..e40459d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,9 @@ ChangeLog for Pylint
* Extend the checking for unbalanced-tuple-unpacking and
unpacking-non-sequence to instance attribute unpacking as well.
+ * Add 'indexing-exception' warning, which detects that indexing
+ an exception occurs in Python 2 (behaviour removed in Python 3).
+
2014-04-18 -- 1.2.0
* Pass the current python paths to pylint process when invoked via
epylint. Fixes BitBucket issue #133.
diff --git a/checkers/exceptions.py b/checkers/exceptions.py
index 64aca5b..84f92ea 100644
--- a/checkers/exceptions.py
+++ b/checkers/exceptions.py
@@ -107,6 +107,11 @@ MSGS = {
'clauses. '
'See http://www.python.org/dev/peps/pep-3110/',
{'maxversion': (3, 0)}),
+ 'W0713': ('Indexing exceptions will not work on Python 3',
+ 'indexing-exception',
+ 'Indexing exceptions will not work on Python 3. Use '
+ '`exception.args[index]` instead.',
+ {'maxversion': (3, 0)}),
}
@@ -211,6 +216,17 @@ class ExceptionsChecker(BaseChecker):
if isinstance(node.name, (astroid.Tuple, astroid.List)):
self.add_message('unpacking-in-except', node=node)
+ @check_messages('indexing-exception')
+ def visit_subscript(self, node):
+ """ Look for indexing exceptions. """
+ try:
+ for infered in node.value.infer():
+ if not isinstance(infered, astroid.Instance):
+ continue
+ if inherit_from_std_ex(infered):
+ self.add_message('indexing-exception', node=node)
+ except astroid.InferenceError:
+ return
@check_messages('bare-except', 'broad-except', 'pointless-except',
'binary-op-exception', 'bad-except-order',
diff --git a/test/input/func_indexing_exceptions_py_30.py b/test/input/func_indexing_exceptions_py_30.py
new file mode 100644
index 0000000..1200631
--- /dev/null
+++ b/test/input/func_indexing_exceptions_py_30.py
@@ -0,0 +1,16 @@
+"""
+Check for indexing exceptions.
+"""
+# pylint: disable=import-error
+__revision__ = 0
+import socket
+from unknown import ExtensionException
+
+class SubException(IndexError):
+ """ empty """
+
+_ = IndexError("test")[0]
+_ = ZeroDivisionError("error")[0]
+_ = ExtensionException("error")[0]
+_ = SubException("error")[1]
+_ = socker.error("socket")[0]
diff --git a/test/messages/func_indexing_exceptions_py_30.txt b/test/messages/func_indexing_exceptions_py_30.txt
new file mode 100644
index 0000000..1ec5412
--- /dev/null
+++ b/test/messages/func_indexing_exceptions_py_30.txt
@@ -0,0 +1,4 @@
+W: 13: Indexing exceptions will not work on Python 3
+W: 14: Indexing exceptions will not work on Python 3
+W: 16: Indexing exceptions will not work on Python 3
+W: 17: Indexing exceptions will not work on Python 3 \ No newline at end of file