Skip to content

Commit 8edce01

Browse files
Don't emit trailing-whitespace twice for multi-line docstrings (#7335)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent e2816cd commit 8edce01

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix double emitting ``trailing-whitespace`` for multi-line docstrings.
2+
3+
Closes #6936

pylint/checkers/format.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
only_required_for_messages,
3030
)
3131
from pylint.constants import WarningScope
32+
from pylint.interfaces import HIGH
3233
from pylint.typing import MessageDefinitionTuple
3334
from pylint.utils.pragma_parser import OPTION_PO, PragmaParserError, parse_pragma
3435

@@ -403,6 +404,12 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
403404
# the full line; therefore we check the next token on the line.
404405
if tok_type == tokenize.INDENT:
405406
self.new_line(TokenWrapper(tokens), idx - 1, idx + 1)
407+
# A tokenizer oddity: if a line contains a multi-line string,
408+
# the NEWLINE also gets its own token which we already checked in
409+
# the multi-line docstring case.
410+
# See https://github.com/PyCQA/pylint/issues/6936
411+
elif tok_type == tokenize.NEWLINE:
412+
pass
406413
else:
407414
self.new_line(TokenWrapper(tokens), idx - 1, idx)
408415

@@ -584,7 +591,10 @@ def check_line_ending(self, line: str, i: int) -> None:
584591
stripped_line = line.rstrip("\t\n\r\v ")
585592
if line[len(stripped_line) :] not in ("\n", "\r\n"):
586593
self.add_message(
587-
"trailing-whitespace", line=i, col_offset=len(stripped_line)
594+
"trailing-whitespace",
595+
line=i,
596+
col_offset=len(stripped_line),
597+
confidence=HIGH,
588598
)
589599

590600
def check_line_length(self, line: str, i: int, checker_off: bool) -> None:
+14-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
"""Regression test for trailing-whitespace (C0303)."""
2-
# pylint: disable=mixed-line-endings
2+
# pylint: disable=mixed-line-endings,pointless-string-statement
33

44
# +1: [trailing-whitespace]
55
print('some trailing whitespace')
66
# +1: [trailing-whitespace]
77
print('trailing whitespace does not count towards the line length limit')
8-
print('windows line ends are ok')
9-
# +1: [trailing-whitespace]
10-
print('but trailing whitespace on win is not')
8+
print('windows line ends are ok')
9+
# +1: [trailing-whitespace]
10+
print('but trailing whitespace on win is not')
11+
12+
# Regression test for https://github.com/PyCQA/pylint/issues/6936
13+
# +2: [trailing-whitespace]
14+
""" This module has the Board class.
15+
"""
16+
17+
# +3: [trailing-whitespace]
18+
""" This module has the Board class.
19+
It's a very nice Board.
20+
"""
+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
trailing-whitespace:5:33:None:None::Trailing whitespace:UNDEFINED
2-
trailing-whitespace:7:73:None:None::Trailing whitespace:UNDEFINED
3-
trailing-whitespace:10:46:None:None::Trailing whitespace:UNDEFINED
1+
trailing-whitespace:5:33:None:None::Trailing whitespace:HIGH
2+
trailing-whitespace:7:73:None:None::Trailing whitespace:HIGH
3+
trailing-whitespace:10:46:None:None::Trailing whitespace:HIGH
4+
trailing-whitespace:15:3:None:None::Trailing whitespace:HIGH
5+
trailing-whitespace:20:3:None:None::Trailing whitespace:HIGH

0 commit comments

Comments
 (0)