diff --git a/doc/whatsnew/fragments/10589.bugfix b/doc/whatsnew/fragments/10589.bugfix new file mode 100644 index 0000000000..65bb985d90 --- /dev/null +++ b/doc/whatsnew/fragments/10589.bugfix @@ -0,0 +1,3 @@ +Scope ``wrong-import-position`` pragma to specific line only. + +Closes #10589 diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index cdda5d9665..c1c8f5ce5d 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -702,9 +702,7 @@ def _check_position(self, node: ImportNode) -> None: # if a first non-import instruction has already been encountered, # it means the import comes after it and therefore is not well placed if self._first_non_import_node: - if self.linter.is_message_enabled( - "wrong-import-position", self._first_non_import_node.fromlineno - ): + if self.linter.is_message_enabled("wrong-import-position", node.fromlineno): self.add_message( "wrong-import-position", node=node, args=node.as_string() ) diff --git a/tests/functional/d/disable_wrong_import_position.py b/tests/functional/d/disable_wrong_import_position.py index 0703325a9e..07b81c0e91 100644 --- a/tests/functional/d/disable_wrong_import_position.py +++ b/tests/functional/d/disable_wrong_import_position.py @@ -1,7 +1,10 @@ -"""Checks that disabling 'wrong-import-position' on a statement prevents it from -invalidating subsequent imports.""" +"""Checks that disabling 'wrong-import-position' only affects the specific line. + +A pragma on a non-import statement should not affect subsequent import statements. +This demonstrates the correct behavior after fixing the bug. +""" # pylint: disable=unused-import CONSTANT = True # pylint: disable=wrong-import-position -import sys +import sys # [wrong-import-position] diff --git a/tests/functional/d/disable_wrong_import_position.txt b/tests/functional/d/disable_wrong_import_position.txt new file mode 100644 index 0000000000..c56c0a200e --- /dev/null +++ b/tests/functional/d/disable_wrong_import_position.txt @@ -0,0 +1 @@ +wrong-import-position:10:0:10:10::"Import ""import sys"" should be placed at the top of the module":UNDEFINED diff --git a/tests/functional/w/wrong_import_position_pragma_scope.py b/tests/functional/w/wrong_import_position_pragma_scope.py new file mode 100644 index 0000000000..e7c7301e5d --- /dev/null +++ b/tests/functional/w/wrong_import_position_pragma_scope.py @@ -0,0 +1,23 @@ +"""Test that wrong-import-position pragma suppression is correctly scoped.""" +# pylint: disable=unused-import,invalid-name + +import logging +import sys + +# This pragma should not affect subsequent import statements +logger = logging.getLogger() # pylint: disable=wrong-import-position +logging.basicConfig(level='DEBUG') + +logger.debug('importing modules...') +import os # [wrong-import-position] +import pathlib # [wrong-import-position] +import random # [wrong-import-position] +logger.debug('done importing') + +# Test that pragma on import line works correctly (this import should not be flagged) +constant_var = "test" +import json # pylint: disable=wrong-import-position + +# Test that subsequent imports are not affected by the pragma above +import csv # [wrong-import-position] +import re # [wrong-import-position] diff --git a/tests/functional/w/wrong_import_position_pragma_scope.txt b/tests/functional/w/wrong_import_position_pragma_scope.txt new file mode 100644 index 0000000000..83393dd4d6 --- /dev/null +++ b/tests/functional/w/wrong_import_position_pragma_scope.txt @@ -0,0 +1,5 @@ +wrong-import-position:12:0:12:9::"Import ""import os"" should be placed at the top of the module":UNDEFINED +wrong-import-position:13:0:13:14::"Import ""import pathlib"" should be placed at the top of the module":UNDEFINED +wrong-import-position:14:0:14:13::"Import ""import random"" should be placed at the top of the module":UNDEFINED +wrong-import-position:22:0:22:10::"Import ""import csv"" should be placed at the top of the module":UNDEFINED +wrong-import-position:23:0:23:9::"Import ""import re"" should be placed at the top of the module":UNDEFINED