From a56f16b7a7e5b7b40a9d613e8cbec671a052f9e3 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 30 Nov 2015 00:46:56 +0200 Subject: Add wrong-import-position to check_messages's decorator arguments for ImportChecker.leave_module This fixes an esoteric bug which occurs when ungrouped-imports and wrong-import-order are disabled and pylint is executed on multiple files. What happens is that without wrong-import-position in check_messages, leave_module will never be called, which means that the first non-import node from other files might leak into the current file, leading to wrong-import-position being emitted by pylint. --- pylint/checkers/imports.py | 3 ++- pylint/test/regrtest_data/import_something.py | 4 ++++ pylint/test/regrtest_data/wrong_import_position.py | 11 +++++++++++ pylint/test/test_self.py | 22 +++++++++++++++++++++- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 pylint/test/regrtest_data/import_something.py create mode 100644 pylint/test/regrtest_data/wrong_import_position.py diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index dea6523..cca4998 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -332,7 +332,8 @@ given file (report RP0402 must not be disabled)'} self.add_message('reimported', node=node, args=(name, node.fromlineno)) - @check_messages('wrong-import-order', 'ungrouped-imports') + @check_messages('wrong-import-order', 'ungrouped-imports', + 'wrong-import-position') def leave_module(self, node): # Check imports are grouped by category (standard, 3rd party, local) std_imports, ext_imports, loc_imports = self._check_imports_order(node) diff --git a/pylint/test/regrtest_data/import_something.py b/pylint/test/regrtest_data/import_something.py new file mode 100644 index 0000000..3a74a71 --- /dev/null +++ b/pylint/test/regrtest_data/import_something.py @@ -0,0 +1,4 @@ +# pylint: disable=missing-docstring,unused-import + +import os +import sys diff --git a/pylint/test/regrtest_data/wrong_import_position.py b/pylint/test/regrtest_data/wrong_import_position.py new file mode 100644 index 0000000..9e2d099 --- /dev/null +++ b/pylint/test/regrtest_data/wrong_import_position.py @@ -0,0 +1,11 @@ +"""Test that wrong-import-position is properly reset when +other errors are disabled. +""" +# pylint: disable=unused-import, too-few-public-methods + + +class Something(object): + """A class before an import.""" + + +import os diff --git a/pylint/test/test_self.py b/pylint/test/test_self.py index 324cc16..69b739c 100644 --- a/pylint/test/test_self.py +++ b/pylint/test/test_self.py @@ -94,7 +94,9 @@ class RunTC(unittest.TestCase): def _run_pylint(self, args, out, reporter=None): with _patch_streams(out): with self.assertRaises(SystemExit) as cm: - Run(args, reporter=reporter) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + Run(args, reporter=reporter) return cm.exception.code def _test_output(self, args, expected_output): @@ -270,6 +272,24 @@ class RunTC(unittest.TestCase): module = join(HERE, 'regrtest_data', 'html_crash_420.py') self._runtest([module], code=16, reporter=HTMLReporter(out)) + def test_wrong_import_position_when_others_disabled(self): + expected_output = textwrap.dedent(''' + No config file found, using default configuration + ************* Module wrong_import_position + C: 11, 0: Import "import os" should be placed at the top of the module (wrong-import-position) + ''') + module1 = join(HERE, 'regrtest_data', 'import_something.py') + module2 = join(HERE, 'regrtest_data', 'wrong_import_position.py') + args = [module2, module1, + "--disable=all", "--enable=wrong-import-position", + "-rn"] + out = six.StringIO() + self._run_pylint(args, out=out) + actual_output = out.getvalue() + self.assertEqual(expected_output.strip(), actual_output.strip()) + + + if __name__ == '__main__': unittest.main() -- cgit v1.2.1