diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2014-11-17 15:39:50 +0200 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2014-11-17 15:39:50 +0200 |
commit | 5d734142fc0d5bdb6b8ae3023e43f40d75e690a0 (patch) | |
tree | 1d9039da39935ab653689fdd90db9650c8fc0045 | |
parent | 0d82f2e58e747dd7189c7d32c68059ef243cdd81 (diff) | |
download | pylint-5d734142fc0d5bdb6b8ae3023e43f40d75e690a0.tar.gz |
Add a new CLI option, '--py3k'.
This option will enable the Python 3 porting
mode. This mode will emit
warnings and errors for constructs
invalid or removed in Python 3,
helping the users of Pylint
to port their projects.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | lint.py | 12 | ||||
-rw-r--r-- | test/functional/indexing_exception.py (renamed from test/input/func_indexing_exceptions_py_30.py) | 6 | ||||
-rw-r--r-- | test/functional/indexing_exception.rc | 5 | ||||
-rw-r--r-- | test/functional/indexing_exception.txt | 3 | ||||
-rw-r--r-- | test/functional/invalid_exceptions_raised.py | 3 | ||||
-rw-r--r-- | test/functional/invalid_exceptions_raised.txt | 8 | ||||
-rw-r--r-- | test/functional/unpacked_exceptions.rc | 3 | ||||
-rw-r--r-- | test/input/func_backtick_deprecated_py_30.py | 4 | ||||
-rw-r--r-- | test/input/func_w0331_py_30.py | 7 | ||||
-rw-r--r-- | test/input/func_w0701_py_30.py | 12 | ||||
-rw-r--r-- | test/messages/func_backtick_deprecated_py_30.txt | 2 | ||||
-rw-r--r-- | test/messages/func_indexing_exceptions_py_30.txt | 3 | ||||
-rw-r--r-- | test/messages/func_w0152_py29.txt | 1 | ||||
-rw-r--r-- | test/messages/func_w0331_py_30.txt | 1 | ||||
-rw-r--r-- | test/messages/func_w0701_py_30.txt | 3 | ||||
-rw-r--r-- | test/test_self.py | 9 | ||||
-rw-r--r-- | test/unittest_checker_python3.py | 40 |
18 files changed, 85 insertions, 43 deletions
@@ -174,6 +174,12 @@ ChangeLog for Pylint * Add support for disabling a checker, by specifying an 'enabled' attribute on the checker class. + + * Add a new CLI option, '--py3k', for enabling the Python 3 porting + mode. This mode will emit warnings and errors for constructs + invalid or removed in Python 3, helping the users of Pylint + to port their projects. + 2014-07-26 -- 1.3.0 @@ -1131,6 +1131,12 @@ group are mutually exclusive.'), 'disabled and for others, only the ERROR messages are ' 'displayed, and no reports are done by default'''}), + ('py3k', + {'action' : 'callback', 'callback' : self.cb_python3_porting_mode, + 'help' : 'In Python 3 porting mode, all checkers will be ' + 'disabled and only messages emitted by the porting ' + 'checker will be displayed'}), + ('profile', {'type' : 'yn', 'metavar' : '<y_or_n>', 'default': False, 'hide': True, @@ -1282,6 +1288,12 @@ group are mutually exclusive.'), print('%-18s: %s' % level) sys.exit(0) + def cb_python3_porting_mode(self, *args, **kwargs): + """Activate only the python3 porting checker.""" + self.linter.disable('all') + self.linter.enable('python3') + + def cb_init_hook(optname, value): """exec arbitrary code to set sys.path for instance""" exec(value) diff --git a/test/input/func_indexing_exceptions_py_30.py b/test/functional/indexing_exception.py index 0147cfc..7ea50d5 100644 --- a/test/input/func_indexing_exceptions_py_30.py +++ b/test/functional/indexing_exception.py @@ -9,7 +9,7 @@ from unknown import ExtensionException class SubException(IndexError): """ empty """ -_ = IndexError("test")[0] -_ = ZeroDivisionError("error")[0] +_ = IndexError("test")[0] # [indexing-exception] +_ = ZeroDivisionError("error")[0] # [indexing-exception] _ = ExtensionException("error")[0] -_ = SubException("error")[1] +_ = SubException("error")[1] # [indexing-exception] diff --git a/test/functional/indexing_exception.rc b/test/functional/indexing_exception.rc new file mode 100644 index 0000000..9540bc9 --- /dev/null +++ b/test/functional/indexing_exception.rc @@ -0,0 +1,5 @@ +[testoptions] +max_pyver=3.0 + +[Messages Control] +enable=python3
\ No newline at end of file diff --git a/test/functional/indexing_exception.txt b/test/functional/indexing_exception.txt new file mode 100644 index 0000000..9f30678 --- /dev/null +++ b/test/functional/indexing_exception.txt @@ -0,0 +1,3 @@ +indexing-exception:12::Indexing exceptions will not work on Python 3
+indexing-exception:13::Indexing exceptions will not work on Python 3
+indexing-exception:15::Indexing exceptions will not work on Python 3
\ No newline at end of file diff --git a/test/functional/invalid_exceptions_raised.py b/test/functional/invalid_exceptions_raised.py index fe6223d..8728581 100644 --- a/test/functional/invalid_exceptions_raised.py +++ b/test/functional/invalid_exceptions_raised.py @@ -28,7 +28,7 @@ def bad_case1(): def bad_case2(): """raise""" - # +2:<3.0:[old-raise-syntax,nonstandard-exception] + # +2:<3.0:[nonstandard-exception] # +1:>=3.0:[raising-non-exception] raise OldStyleClass, 'hop' @@ -38,7 +38,6 @@ def bad_case3(): def bad_case4(): """raise""" - # +1:<3.0:[old-raise-syntax] raise NotImplemented, 'hop' # [notimplemented-raised] def bad_case5(): diff --git a/test/functional/invalid_exceptions_raised.txt b/test/functional/invalid_exceptions_raised.txt index b4e8716..b12f77f 100644 --- a/test/functional/invalid_exceptions_raised.txt +++ b/test/functional/invalid_exceptions_raised.txt @@ -2,10 +2,8 @@ nonstandard-exception:23:bad_case0:"Exception doesn't inherit from standard ""Ex raising-non-exception:23:bad_case0:Raising a new style class which doesn't inherit from BaseException raising-non-exception:27:bad_case1:Raising a new style class which doesn't inherit from BaseException nonstandard-exception:33:bad_case2:"Exception doesn't inherit from standard ""Exception"" class":INFERENCE -old-raise-syntax:33:bad_case2:Use raise ErrorClass(args) instead of raise ErrorClass, args. raising-non-exception:33:bad_case2:Raising a new style class which doesn't inherit from BaseException raising-non-exception:37:bad_case3:Raising a new style class which doesn't inherit from BaseException -notimplemented-raised:42:bad_case4:NotImplemented raised - should raise NotImplementedError -old-raise-syntax:42:bad_case4:Use raise ErrorClass(args) instead of raise ErrorClass, args. -raising-bad-type:46:bad_case5:Raising int while only classes or instances are allowed -raising-bad-type:50:base_case6:Raising NoneType while only classes or instances are allowed +notimplemented-raised:41:bad_case4:NotImplemented raised - should raise NotImplementedError +raising-bad-type:45:bad_case5:Raising int while only classes or instances are allowed +raising-bad-type:49:base_case6:Raising NoneType while only classes or instances are allowed diff --git a/test/functional/unpacked_exceptions.rc b/test/functional/unpacked_exceptions.rc index a650233..9540bc9 100644 --- a/test/functional/unpacked_exceptions.rc +++ b/test/functional/unpacked_exceptions.rc @@ -1,2 +1,5 @@ [testoptions] max_pyver=3.0 + +[Messages Control] +enable=python3
\ No newline at end of file diff --git a/test/input/func_backtick_deprecated_py_30.py b/test/input/func_backtick_deprecated_py_30.py deleted file mode 100644 index db3203b..0000000 --- a/test/input/func_backtick_deprecated_py_30.py +++ /dev/null @@ -1,4 +0,0 @@ -"""W0333: flag backtick as deprecated""" -from __future__ import print_function -__revision__ = None -print(`1`) diff --git a/test/input/func_w0331_py_30.py b/test/input/func_w0331_py_30.py deleted file mode 100644 index 6ee6006..0000000 --- a/test/input/func_w0331_py_30.py +++ /dev/null @@ -1,7 +0,0 @@ -"""check use of <> -""" - -__revision__ = 1 - -if __revision__ <> 2: - __revision__ = 2 diff --git a/test/input/func_w0701_py_30.py b/test/input/func_w0701_py_30.py deleted file mode 100644 index 9c1b727..0000000 --- a/test/input/func_w0701_py_30.py +++ /dev/null @@ -1,12 +0,0 @@ -"""test string exception -""" - -__revision__ = '' - -def function1(): - """hehehe""" - raise "String Exception" - -def function2(): - """hehehe""" - raise 'exception', 'message' diff --git a/test/messages/func_backtick_deprecated_py_30.txt b/test/messages/func_backtick_deprecated_py_30.txt deleted file mode 100644 index a4156f7..0000000 --- a/test/messages/func_backtick_deprecated_py_30.txt +++ /dev/null @@ -1,2 +0,0 @@ -E: 4: Use of the `` operator - diff --git a/test/messages/func_indexing_exceptions_py_30.txt b/test/messages/func_indexing_exceptions_py_30.txt deleted file mode 100644 index 9c86ba5..0000000 --- a/test/messages/func_indexing_exceptions_py_30.txt +++ /dev/null @@ -1,3 +0,0 @@ -W: 12: Indexing exceptions will not work on Python 3
-W: 13: Indexing exceptions will not work on Python 3
-W: 15: Indexing exceptions will not work on Python 3
\ No newline at end of file diff --git a/test/messages/func_w0152_py29.txt b/test/messages/func_w0152_py29.txt index a6cf2f3..484d5bf 100644 --- a/test/messages/func_w0152_py29.txt +++ b/test/messages/func_w0152_py29.txt @@ -1,3 +1,2 @@ W: 5: Used * or ** magic -W: 5: reduce built-in referenced W: 14: Used * or ** magic diff --git a/test/messages/func_w0331_py_30.txt b/test/messages/func_w0331_py_30.txt deleted file mode 100644 index d697fa1..0000000 --- a/test/messages/func_w0331_py_30.txt +++ /dev/null @@ -1 +0,0 @@ -E: 6: Use of the <> operator diff --git a/test/messages/func_w0701_py_30.txt b/test/messages/func_w0701_py_30.txt deleted file mode 100644 index 8ade83d..0000000 --- a/test/messages/func_w0701_py_30.txt +++ /dev/null @@ -1,3 +0,0 @@ -E: 12:function2: Use raise ErrorClass(args) instead of raise ErrorClass, args. -W: 8:function1: Raising a string exception -W: 12:function2: Raising a string exception diff --git a/test/test_self.py b/test/test_self.py index 57f4701..4b5ad29 100644 --- a/test/test_self.py +++ b/test/test_self.py @@ -135,5 +135,14 @@ class RunTC(unittest.TestCase): self._runtest(['-j 2', 'pylint/test/functional/arguments.py', 'pylint/test/functional/bad_continuation.py'], code=1) + def test_py3k_option(self): + # Test that --py3k flag works. + rc_code = 2 if six.PY2 else 0 + self._runtest([join(HERE, 'functional', 'unpacked_exceptions.py'), + '--py3k'], + code=rc_code) + + + if __name__ == '__main__': unittest.main() diff --git a/test/unittest_checker_python3.py b/test/unittest_checker_python3.py index 340470d..8dc265c 100644 --- a/test/unittest_checker_python3.py +++ b/test/unittest_checker_python3.py @@ -16,6 +16,7 @@ from __future__ import absolute_import import sys import unittest +import textwrap from astroid import test_utils @@ -130,6 +131,13 @@ class Python3CheckerTest(testutils.CheckerTestCase): with self.assertAddsMessages(message): self.checker.visit_print(node) + @python2_only + def test_backtick(self): + node = test_utils.extract_node('`test`') + message = testutils.Message('backtick', node=node) + with self.assertAddsMessages(message): + self.checker.visit_backquote(node) + def test_relative_import(self): node = test_utils.extract_node('import string #@') message = testutils.Message('no-absolute-import', node=node) @@ -250,6 +258,31 @@ class Python3CheckerTest(testutils.CheckerTestCase): node = test_utils.extract_node('def func((a, b)):#@\n pass') arg = node.args.args[0] with self.assertAddsMessages(testutils.Message('parameter-unpacking', node=arg)): + self.checker.visit_arguments(node.args) + + @python2_only + def test_old_raise_syntax(self): + node = test_utils.extract_node('raise Exception, "test"') + message = testutils.Message('old-raise-syntax', node=node) + with self.assertAddsMessages(message): + self.checker.visit_raise(node) + + @python2_only + def test_raising_string(self): + node = test_utils.extract_node('raise "Test"') + message = testutils.Message('raising-string', node=node) + with self.assertAddsMessages(message): + self.checker.visit_raise(node) + + @python2_only + def test_checker_disabled_by_default(self): + node = test_utils.build_module(textwrap.dedent(""" + abc = 1l + raise Exception, "test" + raise "test" + `abc` + """)) + with self.assertNoMessages(): self.walk(node) @@ -265,6 +298,13 @@ class Python3TokenCheckerTest(testutils.CheckerTestCase): testutils.Message('long-suffix', line=1)): self.checker.process_tokens(tokens) + @python2_only + def test_old_ne_operator(self): + tokens = testutils.tokenize_str("1 <> 2") + message = testutils.Message('old-ne-operator', line=1) + with self.assertAddsMessages(message): + self.checker.process_tokens(tokens) + if __name__ == '__main__': unittest.main() |