diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-10-02 18:04:34 +0300 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-10-02 18:04:34 +0300 |
commit | aac41d31f63f24e25d6e14e23d68d90b63c52405 (patch) | |
tree | 00c3ec4a31c02fa8cdd1cc73081859ecf7c8e6cc | |
parent | 0a4169614daa0708abdfa05bcdd3ad670e3d94a1 (diff) | |
parent | 7adfb614f2d3b2a84c6a49788d776f97954ca0e2 (diff) | |
download | pylint-aac41d31f63f24e25d6e14e23d68d90b63c52405.tar.gz |
Merged in dmand/pylint/multiple-imports-checker (pull request #279)
Add checker to identify multiple imports on one line.
-rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | pylint/checkers/imports.py | 10 | ||||
-rw-r--r-- | pylint/test/functional/bad_exception_context.py | 2 | ||||
-rw-r--r-- | pylint/test/functional/invalid_exceptions_caught.py | 2 | ||||
-rw-r--r-- | pylint/test/functional/multiple_imports.py | 2 | ||||
-rw-r--r-- | pylint/test/functional/multiple_imports.txt | 1 | ||||
-rw-r--r-- | pylint/test/unittest_checker_imports.py | 19 |
8 files changed, 38 insertions, 5 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 518a7a0..ee64a35 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -70,3 +70,5 @@ Order doesn't matter (not that much, at least ;) * Cezar Elnazli: deprecated-method * Stéphane Wirtel: nonlocal-without-binding + +* Dmitry Pribysh: multiple-imports. @@ -2,9 +2,12 @@ ChangeLog for Pylint -------------------- -- + * Add checker to identify multiple imports on one line. + Closes issue #598. + * Fix unused-argument false positive when the "+=" operator is used. Closes issue #518. - + * Don't emit import-error for ignored modules. PyLint will not emit import errors for any import which is, or is a subpackage of, a module in the ignored-modules list. Closes issue #223. diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index a5a2f28..0be2b2c 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -173,6 +173,11 @@ MSGS = { 'misplaced-future', 'Python 2.5 and greater require __future__ import to be the \ first non docstring statement in the module.'), + + 'C0410': ('Multiple imports on one line (%s)', + 'multiple-imports', + 'Used when import statement importing multiple modules is ' + 'detected.'), } class ImportsChecker(BaseChecker): @@ -254,7 +259,10 @@ given file (report RP0402 must not be disabled)'} def visit_import(self, node): """triggered when an import statement is seen""" modnode = node.root() - for name, _ in node.names: + names = [name for name, _ in node.names] + if len(names) >= 2: + self.add_message('multiple-imports', args=', '.join(names), node=node) + for name in names: self._check_deprecated_module(node, name) importedmodnode = self.get_imported_module(node, name) if importedmodnode is None: diff --git a/pylint/test/functional/bad_exception_context.py b/pylint/test/functional/bad_exception_context.py index f386343..af3e4fb 100644 --- a/pylint/test/functional/bad_exception_context.py +++ b/pylint/test/functional/bad_exception_context.py @@ -1,6 +1,6 @@ """Check that raise ... from .. uses a proper exception context """ -# pylint: disable=unreachable, import-error +# pylint: disable=unreachable, import-error, multiple-imports import socket, unknown diff --git a/pylint/test/functional/invalid_exceptions_caught.py b/pylint/test/functional/invalid_exceptions_caught.py index 2392ab0..545954d 100644 --- a/pylint/test/functional/invalid_exceptions_caught.py +++ b/pylint/test/functional/invalid_exceptions_caught.py @@ -1,5 +1,5 @@ """Test for catching non-exceptions.""" -# pylint: disable=too-many-ancestors, no-absolute-import, import-error +# pylint: disable=too-many-ancestors, no-absolute-import, import-error, multiple-imports from __future__ import print_function import socket, binascii diff --git a/pylint/test/functional/multiple_imports.py b/pylint/test/functional/multiple_imports.py new file mode 100644 index 0000000..9008dcc --- /dev/null +++ b/pylint/test/functional/multiple_imports.py @@ -0,0 +1,2 @@ +# pylint: disable=missing-docstring, unused-import +import os, socket # [multiple-imports] diff --git a/pylint/test/functional/multiple_imports.txt b/pylint/test/functional/multiple_imports.txt new file mode 100644 index 0000000..a5df51e --- /dev/null +++ b/pylint/test/functional/multiple_imports.txt @@ -0,0 +1 @@ +multiple-imports:2::Multiple imports on one line (os, socket) diff --git a/pylint/test/unittest_checker_imports.py b/pylint/test/unittest_checker_imports.py index 8832878..f5e6bbb 100644 --- a/pylint/test/unittest_checker_imports.py +++ b/pylint/test/unittest_checker_imports.py @@ -10,7 +10,10 @@ class ImportsCheckerTC(CheckerTestCase): CHECKER_CLASS = imports.ImportsChecker - @set_config(ignored_modules=('external_module', 'fake_module.submodule')) + @set_config(ignored_modules=('external_module', + 'fake_module.submodule', + 'foo', + 'bar')) def test_import_error_skipped(self): """Make sure that imports do not emit a 'import-error' when the module is configured to be ignored.""" @@ -45,6 +48,20 @@ class ImportsCheckerTC(CheckerTestCase): with self.assertNoMessages(): self.checker.visit_importfrom(node) + node = test_utils.extract_node(""" + import foo, bar + """) + msg = Message('multiple-imports', node=node, args='foo, bar') + with self.assertAddsMessages(msg): + self.checker.visit_import(node) + + node = test_utils.extract_node(""" + import foo + import bar + """) + with self.assertNoMessages(): + self.checker.visit_import(node) + def test_visit_importfrom(self): """ Test that duplicate imports on single line raise 'reimported'. |