diff --git a/ChangeLog b/ChangeLog index e6c623a53f..3649c8c7af 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,12 @@ What's New in Pylint 2.0? Release date: tba + * Disabling 'wrong-import-order', 'wrong-import-position', or + 'ungrouped-imports' for a single line now prevents that line from + triggering violations on subsequent lines. + + Close #1336 + * Added a new error, 'used-prior-global-declaration', which is emitted when a name is used prior a global declaration in a function. This causes a SyntaxError in Python 3.6 diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index d2970eb862..7c1a70afe2 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -425,6 +425,8 @@ def leave_module(self, node): met = set() current_package = None for import_node, import_name in std_imports + ext_imports + loc_imports: + if not self.linter.is_message_enabled('ungrouped-imports', import_node.fromlineno): + continue package, _, _ = import_name.partition('.') if current_package and current_package != package and package in met: self.add_message('ungrouped-imports', node=import_node, @@ -436,6 +438,8 @@ def leave_module(self, node): self._first_non_import_node = None def compute_first_non_import_node(self, node): + if not self.linter.is_message_enabled('wrong-import-position', node.fromlineno): + return # if the node does not contain an import instruction, and if it is the # first node of the module, keep a track of it (all the import positions # of the module will be compared to the position of this first @@ -466,6 +470,8 @@ def compute_first_non_import_node(self, node): compute_first_non_import_node def visit_functiondef(self, node): + if not self.linter.is_message_enabled('wrong-import-position', node.fromlineno): + return # If it is the first non import instruction of the module, record it. if self._first_non_import_node: return @@ -552,8 +558,8 @@ def _check_imports_order(self, _module_node): extern_imports = [] local_imports = [] std_imports = [] - extern_not_nested = [] - local_not_nested = [] + extern_not_ignored = [] + local_not_ignored = [] isort_obj = isort.SortImports( file_contents='', known_third_party=self.config.known_third_party, known_standard_library=self.config.known_standard_library, @@ -564,10 +570,12 @@ def _check_imports_order(self, _module_node): else: package = modname.split('.')[0] nested = not isinstance(node.parent, astroid.Module) + ignore_for_import_order = not self.linter.is_message_enabled('wrong-import-order', + node.fromlineno) import_category = isort_obj.place_module(package) if import_category in ('FUTURE', 'STDLIB'): std_imports.append((node, package)) - wrong_import = extern_not_nested or local_not_nested + wrong_import = extern_not_ignored or local_not_ignored if self._is_fallback_import(node, wrong_import): continue if wrong_import and not nested: @@ -576,17 +584,17 @@ def _check_imports_order(self, _module_node): '"%s"' % wrong_import[0][0].as_string())) elif import_category in ('FIRSTPARTY', 'THIRDPARTY'): extern_imports.append((node, package)) - if not nested: - extern_not_nested.append((node, package)) - wrong_import = local_not_nested + if not nested and not ignore_for_import_order: + extern_not_ignored.append((node, package)) + wrong_import = local_not_ignored if wrong_import and not nested: self.add_message('wrong-import-order', node=node, args=('external import "%s"' % node.as_string(), '"%s"' % wrong_import[0][0].as_string())) elif import_category == 'LOCALFOLDER': local_imports.append((node, package)) - if not nested: - local_not_nested.append((node, package)) + if not nested and not ignore_for_import_order: + local_not_ignored.append((node, package)) return std_imports, extern_imports, local_imports def _get_imported_module(self, importnode, modname): diff --git a/pylint/test/functional/disable_ungrouped_imports.py b/pylint/test/functional/disable_ungrouped_imports.py new file mode 100644 index 0000000000..cfcc01f82a --- /dev/null +++ b/pylint/test/functional/disable_ungrouped_imports.py @@ -0,0 +1,10 @@ +"""Checks that disabling 'ungrouped-imports' on an import prevents subsequent +imports from being considered ungrouped in respect to it.""" +# pylint: disable=unused-import,relative-import,wrong-import-position,wrong-import-order,using-constant-test +# pylint: disable=import-error +import os +import logging.config # pylint: disable=ungrouped-imports +import os.path +import logging +from os.path import join # [ungrouped-imports] +import logging.handlers # [ungrouped-imports] diff --git a/pylint/test/functional/disable_ungrouped_imports.txt b/pylint/test/functional/disable_ungrouped_imports.txt new file mode 100644 index 0000000000..9f2fa443f9 --- /dev/null +++ b/pylint/test/functional/disable_ungrouped_imports.txt @@ -0,0 +1,2 @@ +ungrouped-imports:9::Imports from package os are not grouped +ungrouped-imports:10::Imports from package logging are not grouped diff --git a/pylint/test/functional/disable_wrong_import_order.py b/pylint/test/functional/disable_wrong_import_order.py new file mode 100644 index 0000000000..f725d482af --- /dev/null +++ b/pylint/test/functional/disable_wrong_import_order.py @@ -0,0 +1,11 @@ +"""Checks that disabling 'wrong-import-order' on an import prevents subsequent +imports from being considered out-of-order in respect to it but does not prevent +it from being considered for 'ungrouped-imports'.""" +# pylint: disable=unused-import,import-error,no-name-in-module + +from first_party.foo import bar # pylint: disable=wrong-import-order +import logging +import os.path +import sys +from astroid import are_exclusive +import first_party # [ungrouped-imports] diff --git a/pylint/test/functional/disable_wrong_import_order.txt b/pylint/test/functional/disable_wrong_import_order.txt new file mode 100644 index 0000000000..df71711fce --- /dev/null +++ b/pylint/test/functional/disable_wrong_import_order.txt @@ -0,0 +1 @@ +ungrouped-imports:11::Imports from package first_party are not grouped diff --git a/pylint/test/functional/disable_wrong_import_position.py b/pylint/test/functional/disable_wrong_import_position.py new file mode 100644 index 0000000000..0703325a9e --- /dev/null +++ b/pylint/test/functional/disable_wrong_import_position.py @@ -0,0 +1,7 @@ +"""Checks that disabling 'wrong-import-position' on a statement prevents it from +invalidating subsequent imports.""" +# pylint: disable=unused-import + +CONSTANT = True # pylint: disable=wrong-import-position + +import sys