Skip to content

Commit 8cace41

Browse files
fix: scope wrong-import-position pragma to specific line only
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
1 parent 68ab16f commit 8cace41

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

pylint/checkers/imports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def _check_position(self, node: ImportNode) -> None:
703703
# it means the import comes after it and therefore is not well placed
704704
if self._first_non_import_node:
705705
if self.linter.is_message_enabled(
706-
"wrong-import-position", self._first_non_import_node.fromlineno
706+
"wrong-import-position", node.fromlineno
707707
):
708708
self.add_message(
709709
"wrong-import-position", node=node, args=node.as_string()
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
"""Checks that disabling 'wrong-import-position' on a statement prevents it from
2-
invalidating subsequent imports."""
1+
"""Checks that disabling 'wrong-import-position' only affects the specific line.
2+
3+
A pragma on a non-import statement should not affect subsequent import statements.
4+
This demonstrates the correct behavior after fixing the bug.
5+
"""
36
# pylint: disable=unused-import
47

58
CONSTANT = True # pylint: disable=wrong-import-position
69

7-
import sys
10+
import sys # [wrong-import-position]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wrong-import-position:10:0:10:10::"Import ""import sys"" should be placed at the top of the module":UNDEFINED
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Test that wrong-import-position pragma suppression is correctly scoped."""
2+
# pylint: disable=unused-import,invalid-name
3+
4+
import logging
5+
import sys
6+
7+
# This pragma should not affect subsequent import statements
8+
logger = logging.getLogger() # pylint: disable=wrong-import-position
9+
logging.basicConfig(level='DEBUG')
10+
11+
logger.debug('importing modules...')
12+
import os # [wrong-import-position]
13+
import pathlib # [wrong-import-position]
14+
import random # [wrong-import-position]
15+
logger.debug('done importing')
16+
17+
# Test that pragma on import line works correctly (this import should not be flagged)
18+
constant_var = "test"
19+
import json # pylint: disable=wrong-import-position
20+
21+
# Test that subsequent imports are not affected by the pragma above
22+
import csv # [wrong-import-position]
23+
import re # [wrong-import-position]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
wrong-import-position:19:0:19:9::"Import ""import os"" should be placed at the top of the module":UNDEFINED
2+
wrong-import-position:20:0:20:14::"Import ""import pathlib"" should be placed at the top of the module":UNDEFINED
3+
wrong-import-position:21:0:21:13::"Import ""import random"" should be placed at the top of the module":UNDEFINED
4+
wrong-import-position:29:0:29:10::"Import ""import csv"" should be placed at the top of the module":UNDEFINED
5+
wrong-import-position:30:0:30:9::"Import ""import re"" should be placed at the top of the module":UNDEFINED

0 commit comments

Comments
 (0)