From dde7afa51359b0a51b0a336a3dfbce4da0d719ee Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 25 Jun 2015 23:42:10 +0300 Subject: Add a new error for the Python 3 porting checker, `import-star-module-level`. This error is used when a star import is detected in another scope than the module level, which is an error on Python 3. Using this will emit a SyntaxWarning on Python 2. --- ChangeLog | 5 +++++ pylint/checkers/python3.py | 10 +++++++++- pylint/test/unittest_checker_python3.py | 10 ++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 800e8c4..745cb18 100644 --- a/ChangeLog +++ b/ChangeLog @@ -187,6 +187,11 @@ ChangeLog for Pylint * Detect a couple of objects which can't be base classes (bool, slice, range and memoryview, which weren't detected until now). + + * Add a new error for the Python 3 porting checker, `import-star-module-level`, + which is used when a star import is detected in another scope than the + module level, which is an error on Python 3. Using this will emit a + SyntaxWarning on Python 2. diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py index 95aa3a7..6738ec6 100644 --- a/pylint/checkers/python3.py +++ b/pylint/checkers/python3.py @@ -130,6 +130,11 @@ class Python3Checker(checkers.BaseChecker): {'scope': WarningScope.NODE, 'maxversion': (3, 0), 'old_names': [('W0333', 'backtick')]}), + 'E1609': ('Import * only allowed at module level', + 'import-star-module-level', + 'Used when the import star syntax is used somewhere ' + 'else than the module level.', + {'maxversion': (3, 0)}), 'W1601': ('apply built-in referenced', 'apply-builtin', 'Used when the apply built-in function is referenced ' @@ -403,7 +408,7 @@ class Python3Checker(checkers.BaseChecker): def visit_print(self, node): self.add_message('print-statement', node=node) - @utils.check_messages('no-absolute-import') + @utils.check_messages('no-absolute-import', 'import-star-module-level') def visit_from(self, node): if node.modname == '__future__': for name, _ in node.names: @@ -413,6 +418,9 @@ class Python3Checker(checkers.BaseChecker): self._future_absolute_import = True elif not self._future_absolute_import: self.add_message('no-absolute-import', node=node) + if node.names[0][0] == '*': + if not isinstance(node.scope(), astroid.Module): + self.add_message('import-star-module-level', node=node) @utils.check_messages('no-absolute-import') def visit_import(self, node): diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py index 7d7b052..57a4459 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -258,6 +258,16 @@ class Python3CheckerTest(testutils.CheckerTestCase): for module in (module_import, module_from): self.walk(module) + def test_import_star_module_level(self): + node = test_utils.extract_node(''' + def test(): + from lala import * #@ + ''') + absolute = testutils.Message('no-absolute-import', node=node) + star = testutils.Message('import-star-module-level', node=node) + with self.assertAddsMessages(absolute, star): + self.checker.visit_from(node) + def test_division(self): node = test_utils.extract_node('3 / 2 #@') message = testutils.Message('old-division', node=node) -- cgit v1.2.1