diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-04-17 09:46:03 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-04-17 09:46:03 +0200 |
commit | 91614bcec4142b4c718786e7906147e34cdb25ad (patch) | |
tree | 62c951e1e12ddcbd4c08e265e400bfe2d2c008ad | |
parent | 0de52249820dcfa51dd7c42b3a48c40b0a6bb477 (diff) | |
download | pylint-git-91614bcec4142b4c718786e7906147e34cdb25ad.tar.gz |
Added a new Python 2/3 check for invalid lambda parameter list, `lambda-parameter-parens`
Close #1995
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/whatsnew/1.9.rst | 8 | ||||
-rw-r--r-- | pylint/checkers/python3.py | 11 | ||||
-rw-r--r-- | pylint/test/unittest_checker_python3.py | 5 |
4 files changed, 28 insertions, 0 deletions
@@ -7,6 +7,10 @@ What's New in Pylint 1.9.0? Release date: + * Added a new Python 2/3 check for invalid lambda parameter list, `lambda-parameter-parens` + + Close #1995 + * Added a new Python 2/3 check for accessing `operator.div`, which is removed in Python 3 Close #1936 diff --git a/doc/whatsnew/1.9.rst b/doc/whatsnew/1.9.rst index 6ddc30d2d..f9a349961 100644 --- a/doc/whatsnew/1.9.rst +++ b/doc/whatsnew/1.9.rst @@ -31,5 +31,13 @@ New checkers from six.moves.urllib.parse import urlencode +* A new Python 3 checker was added to warn about invalid lambda parameter list when + using parantheses. This is invalid in Python 3 and will result in a SyntaxError: + + .. code-block:: python + + lambda (some_param, some_other_param): ... + + Other Changes ============= diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py index 5b9cabaaa..e3854dae1 100644 --- a/pylint/checkers/python3.py +++ b/pylint/checkers/python3.py @@ -970,6 +970,11 @@ class Python3TokenChecker(checkers.BaseTokenChecker): 'Used when non-ascii bytes literals are found in a program. ' 'They are no longer supported in Python 3.', {'maxversion': (3, 0)}), + 'E1611': ('Drop invalid lambda parantheses', + 'lambda-parameter-parens', + 'Used when a lambda definition contains parantheses for the parameter ' + 'list. This is invalid syntax in Python 3.X.', + {'maxversion': (3, 0)}), } def process_tokens(self, tokens): @@ -986,6 +991,12 @@ 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]) 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 a0bad865a..5e3fa7c74 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -975,3 +975,8 @@ class TestPython3TokenChecker(testutils.CheckerTestCase): tokens = testutils._tokenize_str(code) with self.assertNoMessages(): self.checker.process_tokens(tokens) + + def test_drop_lambda_parens(self): + 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") |