Skip to content

Commit

Permalink
Fix false-positive useless-suppression for line-too-long
Browse files Browse the repository at this point in the history
Also changes ``add_ignored_message()`` to make ``nodes`` parameter
optional.
Closes pylint-dev#4212
  • Loading branch information
DanielNoord committed Sep 27, 2021
1 parent 162cbfe commit 83fc697
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Release date: TBA

Closes #5066

* Fix false-positive ``useless-suppression`` for ``line-too-long``

Closes #4212


What's New in Pylint 2.11.1?
============================
Expand Down
13 changes: 8 additions & 5 deletions pylint/checkers/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,18 @@ def check_line_ending(self, line: str, i: int) -> None:
"trailing-whitespace", line=i, col_offset=len(stripped_line)
)

def check_line_length(self, line: str, i: int) -> None:
def check_line_length(self, line: str, i: int, checker_off: bool) -> None:
"""
Check that the line length is less than the authorized value
"""
max_chars = self.config.max_line_length
ignore_long_line = self.config.ignore_long_lines
line = line.rstrip()
if len(line) > max_chars and not ignore_long_line.search(line):
self.add_message("line-too-long", line=i, args=(len(line), max_chars))
if checker_off:
self.linter.add_ignored_message("line-too-long", i)
else:
self.add_message("line-too-long", line=i, args=(len(line), max_chars))

@staticmethod
def remove_pylint_option_from_lines(options_pattern_obj) -> str:
Expand Down Expand Up @@ -775,16 +778,16 @@ def check_lines(self, lines: str, lineno: int) -> None:

# Line length check may be deactivated through `pylint: disable` comment
mobj = OPTION_PO.search(lines)
checker_off = False
if mobj:
if not self.is_line_length_check_activated(mobj):
# the line length check is deactivated, we can stop here
return
checker_off = True
# The 'pylint: disable whatever' should not be taken into account for line length count
lines = self.remove_pylint_option_from_lines(mobj)

# here we re-run specific_splitlines since we have filtered out pylint options above
for offset, line in enumerate(self.specific_splitlines(lines)):
self.check_line_length(line, lineno + offset)
self.check_line_length(line, lineno + offset, checker_off)

def check_indent_level(self, string, expected, line_num):
"""return the indent level of the string"""
Expand Down
2 changes: 1 addition & 1 deletion pylint/message/message_handler_mix_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def add_ignored_message( # type: ignore # MessagesHandlerMixIn is always mixed
self: "PyLinter",
msgid: str,
line: int,
node: nodes.NodeNG,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[Confidence] = UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage
Expand Down
11 changes: 8 additions & 3 deletions tests/functional/u/useless/useless_suppression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Test for useless suppression false positive for wrong-import-order
Reported in https://github.com/PyCQA/pylint/issues/2366"""
# pylint: enable=useless-suppression
"""Test for useless suppressions"""
# pylint: enable=useless-suppression, line-too-long
# pylint: disable=unused-import, wrong-import-order

# False positive for wrong-import-order
# Reported in https://github.com/PyCQA/pylint/issues/2366
from pylint import run_pylint
import astroid

# False-positive for 'line-too-long'
VAR = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # pylint: disable=line-too-long

0 comments on commit 83fc697

Please sign in to comment.