Skip to content

Commit

Permalink
Fix useless-suppression for wrong-import-order (#5063)
Browse files Browse the repository at this point in the history
This also adds a new method to ``MessagesHandlerMixIn`` which
adds messages to the list of the ignored messages without doing anything
else. This can be used to avoid ``useless-suppression`` false positives.
This closes #2366
  • Loading branch information
DanielNoord committed Sep 23, 2021
1 parent 9d0b282 commit 09a0b50
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Release date: TBA
* Fix false-positive ``undefined-variable`` with ``Lambda``, ``IfExp``, and
assignment expression.

* Fix false-positive ``useless-suppression`` for ``wrong-import-order``

Closes #2366


What's New in Pylint 2.11.1?
============================
Expand Down
27 changes: 21 additions & 6 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,13 @@ def _check_imports_order(self, _module_node):
elif import_category == "THIRDPARTY":
third_party_imports.append(node_and_package_import)
external_imports.append(node_and_package_import)
if not nested and not ignore_for_import_order:
third_party_not_ignored.append(node_and_package_import)
if not nested:
if not ignore_for_import_order:
third_party_not_ignored.append(node_and_package_import)
else:
self.linter.add_ignored_message(
"wrong-import-order", node.fromlineno, node
)
wrong_import = first_party_not_ignored or local_not_ignored
if wrong_import and not nested:
self.add_message(
Expand All @@ -770,8 +775,13 @@ def _check_imports_order(self, _module_node):
elif import_category == "FIRSTPARTY":
first_party_imports.append(node_and_package_import)
external_imports.append(node_and_package_import)
if not nested and not ignore_for_import_order:
first_party_not_ignored.append(node_and_package_import)
if not nested:
if not ignore_for_import_order:
first_party_not_ignored.append(node_and_package_import)
else:
self.linter.add_ignored_message(
"wrong-import-order", node.fromlineno, node
)
wrong_import = local_not_ignored
if wrong_import and not nested:
self.add_message(
Expand All @@ -784,8 +794,13 @@ def _check_imports_order(self, _module_node):
)
elif import_category == "LOCALFOLDER":
local_imports.append((node, package))
if not nested and not ignore_for_import_order:
local_not_ignored.append((node, package))
if not nested:
if not ignore_for_import_order:
local_not_ignored.append((node, package))
else:
self.linter.add_ignored_message(
"wrong-import-order", node.fromlineno, node
)
return std_imports, external_imports, local_imports

def _get_imported_module(self, importnode, modname):
Expand Down
25 changes: 25 additions & 0 deletions pylint/message/message_handler_mix_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ def add_message( # type: ignore # MessagesHandlerMixIn is always mixed with PyL
message_definition, line, node, args, confidence, col_offset
)

def add_ignored_message( # type: ignore # MessagesHandlerMixIn is always mixed with PyLinter
self: "PyLinter",
msgid: str,
line: int,
node: nodes.NodeNG,
confidence: Optional[Confidence] = UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self.check_message_definition(message_definition, line, node)
self.file_state.handle_ignored_message(
self.get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)

@staticmethod
def check_message_definition(message_definition, line, node):
if message_definition.msgid[0] not in _SCOPE_EXEMPT:
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/u/useless/useless_suppression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Test for useless suppression false positive for wrong-import-order
Reported in https://github.com/PyCQA/pylint/issues/2366"""
# pylint: enable=useless-suppression
# pylint: disable=unused-import, wrong-import-order
from pylint import run_pylint
import astroid

0 comments on commit 09a0b50

Please sign in to comment.