summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2018-04-17 10:10:26 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2018-04-17 10:10:26 +0200
commita36bab11c57cb0eae1c17fcbea3a9c4c943921b6 (patch)
tree33a2ae93c0ab01d7374beea8a56f97c94c6f708f
parent91614bcec4142b4c718786e7906147e34cdb25ad (diff)
downloadpylint-git-a36bab11c57cb0eae1c17fcbea3a9c4c943921b6.tar.gz
Added a new Python 2/3 check for invalid raise syntax.
Close #1937
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/1.9.rst14
-rw-r--r--pylint/checkers/python3.py24
-rw-r--r--pylint/test/unittest_checker_python3.py19
4 files changed, 55 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index d26960237..1be0b2d7c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@ What's New in Pylint 1.9.0?
Release date:
+ * Added a new Python 2/3 check for invalid raise syntax.
+
+ Close #1937
+
* Added a new Python 2/3 check for invalid lambda parameter list, `lambda-parameter-parens`
Close #1995
diff --git a/doc/whatsnew/1.9.rst b/doc/whatsnew/1.9.rst
index f9a349961..18c01b7a2 100644
--- a/doc/whatsnew/1.9.rst
+++ b/doc/whatsnew/1.9.rst
@@ -39,5 +39,19 @@ New checkers
lambda (some_param, some_other_param): ...
+* A new Python 3 checker was added to warn about invalid raise syntax:
+
+ .. code-block:: python
+
+ raise Exception, "value", tb
+
+
+ To have this working on Python 3 as well, please use the ``six`` library:
+
+ .. code-block:: python
+
+ six.reraise(Exception, "value", tb)
+
+
Other Changes
=============
diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py
index e3854dae1..2c98303ad 100644
--- a/pylint/checkers/python3.py
+++ b/pylint/checkers/python3.py
@@ -975,6 +975,11 @@ class Python3TokenChecker(checkers.BaseTokenChecker):
'Used when a lambda definition contains parantheses for the parameter '
'list. This is invalid syntax in Python 3.X.',
{'maxversion': (3, 0)}),
+ 'E1612': ('Cannot raise on Python 3 using this raise form',
+ 'invalid-raise-syntax',
+ 'Used when an invalid raise construct that will not work on Python 3 was '
+ 'deteted.',
+ {'maxversion': (3, 0)}),
}
def process_tokens(self, tokens):
@@ -991,12 +996,19 @@ class Python3TokenChecker(checkers.BaseTokenChecker):
if any(elem for elem in token if ord(elem) > 127):
self.add_message('non-ascii-bytes-literal', line=start[0])
- if (tok_type == tokenize.NAME
- and token == 'lambda'
- and idx < len(tokens)
- and tokens[idx + 1][0] == tokenize.OP
- and tokens[idx + 1][1] == '('):
- self.add_message('lambda-parameter-parens', line=start[0])
+ if tok_type == tokenize.NAME:
+ if (token == 'lambda'
+ and idx < len(tokens)
+ and tokens[idx + 1][0] == tokenize.OP
+ and tokens[idx + 1][1] == '('):
+ self.add_message('lambda-parameter-parens', line=start[0])
+ elif (token == 'raise'
+ and idx < len(tokens) - 1
+ and tokens[idx + 1][0] == tokenize.NAME
+ and tokens[idx + 2][0] == tokenize.OP
+ and tokens[idx + 2][1] == ','):
+ self.add_message('invalid-raise-syntax', line=start[0])
+
def register(linter):
linter.register_checker(Python3Checker(linter))
diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py
index 5e3fa7c74..7d44617c2 100644
--- a/pylint/test/unittest_checker_python3.py
+++ b/pylint/test/unittest_checker_python3.py
@@ -980,3 +980,22 @@ class TestPython3TokenChecker(testutils.CheckerTestCase):
self._test_token_message("lambda (a): a", "lambda-parameter-parens")
self._test_token_message("lambda(a): a", "lambda-parameter-parens")
self._test_token_message("lambda (a, b, c): a", "lambda-parameter-parens")
+
+ def test_invalid_raise_syntax(self):
+ self._test_token_message("raise Exception,'1', None", "invalid-raise-syntax")
+ self._test_token_message("raise Exception,'1', None", "invalid-raise-syntax")
+ self._test_token_message("raise Exception , '1', None", "invalid-raise-syntax")
+
+ codes = [
+ "raise Exception",
+ "raise Exception;",
+ "raise 1",
+ "raise a",
+ "raise",
+ "raise Exception(2)",
+ "raise Exception('abc')",
+ ]
+ for code in codes:
+ tokens = testutils._tokenize_str(code)
+ with self.assertNoMessages():
+ self.checker.process_tokens(tokens)