Skip to content

Commit d1f5eaa

Browse files
Avoid reporting useless-suppression on wrong-import-position (#5219) (#6347)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent fd76004 commit d1f5eaa

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ Release date: TBA
234234

235235
Closes #4525
236236

237+
* Fix falsely issuing ``useless-suppression`` on the ``wrong-import-position`` checker.
238+
239+
Closes #5219
240+
237241
* Fix false negative for ``no-member`` when attempting to assign an instance
238242
attribute to itself without any prior assignment.
239243

doc/whatsnew/2.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ Other Changes
245245

246246
Closes #5205
247247

248+
* Fix falsely issuing ``useless-suppression`` on the ``wrong-import-position`` checker.
249+
250+
Closes #5219
251+
248252
* Fix false negative for ``no-member`` when attempting to assign an instance
249253
attribute to itself without any prior assignment.
250254

pylint/checkers/imports.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,6 @@ def leave_module(self, node: nodes.Module) -> None:
542542
self._first_non_import_node = None
543543

544544
def compute_first_non_import_node(self, node):
545-
if not self.linter.is_message_enabled("wrong-import-position", node.fromlineno):
546-
return
547545
# if the node does not contain an import instruction, and if it is the
548546
# first node of the module, keep a track of it (all the import positions
549547
# of the module will be compared to the position of this first
@@ -584,8 +582,6 @@ def compute_first_non_import_node(self, node):
584582
) = visit_comprehension = visit_expr = visit_if = compute_first_non_import_node
585583

586584
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
587-
if not self.linter.is_message_enabled("wrong-import-position", node.fromlineno):
588-
return
589585
# If it is the first non import instruction of the module, record it.
590586
if self._first_non_import_node:
591587
return
@@ -636,7 +632,16 @@ def _check_position(self, node):
636632
# if a first non-import instruction has already been encountered,
637633
# it means the import comes after it and therefore is not well placed
638634
if self._first_non_import_node:
639-
self.add_message("wrong-import-position", node=node, args=node.as_string())
635+
if self.linter.is_message_enabled(
636+
"wrong-import-position", self._first_non_import_node.fromlineno
637+
):
638+
self.add_message(
639+
"wrong-import-position", node=node, args=node.as_string()
640+
)
641+
else:
642+
self.linter.add_ignored_message(
643+
"wrong-import-position", node.fromlineno, node
644+
)
640645

641646
def _record_import(self, node, importedmodnode):
642647
"""Record the package `node` imports from."""

tests/functional/u/useless/useless_suppression.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for useless suppressions"""
22
# pylint: enable=useless-suppression, line-too-long
3-
# pylint: disable=unused-import, wrong-import-order
3+
# pylint: disable=unused-import, wrong-import-order, wrong-import-position
44

55
# False positive for wrong-import-order
66
# Reported in https://github.com/PyCQA/pylint/issues/2366
@@ -10,3 +10,7 @@
1010
# False-positive for 'line-too-long'
1111
# Reported in https://github.com/PyCQA/pylint/issues/4212
1212
VAR = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # pylint: disable=line-too-long
13+
14+
# False-positive for 'wrong-import-order'
15+
# Reported in https://github.com/PyCQA/pylint/issues/5219
16+
import os

0 commit comments

Comments
 (0)