diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-08-22 16:03:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-22 16:03:28 +0200 |
commit | 8edce0174c576db69808fbe68150f75369d0550a (patch) | |
tree | fd00a03b9eff8024ebc62ef458e7cb7e59f63d54 | |
parent | e2816cd5c6f78b9fd399b35393e54eaa990b70ed (diff) | |
download | pylint-git-8edce0174c576db69808fbe68150f75369d0550a.tar.gz |
Don't emit ``trailing-whitespace`` twice for multi-line docstrings (#7335)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r-- | doc/whatsnew/fragments/6936.false_positive | 3 | ||||
-rw-r--r-- | pylint/checkers/format.py | 12 | ||||
-rw-r--r-- | tests/functional/t/trailing_whitespaces.py | 18 | ||||
-rw-r--r-- | tests/functional/t/trailing_whitespaces.txt | 8 |
4 files changed, 33 insertions, 8 deletions
diff --git a/doc/whatsnew/fragments/6936.false_positive b/doc/whatsnew/fragments/6936.false_positive new file mode 100644 index 000000000..6e7a015ee --- /dev/null +++ b/doc/whatsnew/fragments/6936.false_positive @@ -0,0 +1,3 @@ +Fix double emitting ``trailing-whitespace`` for multi-line docstrings. + +Closes #6936 diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py index cf490d9d8..5d9a854b9 100644 --- a/pylint/checkers/format.py +++ b/pylint/checkers/format.py @@ -29,6 +29,7 @@ from pylint.checkers.utils import ( only_required_for_messages, ) from pylint.constants import WarningScope +from pylint.interfaces import HIGH from pylint.typing import MessageDefinitionTuple from pylint.utils.pragma_parser import OPTION_PO, PragmaParserError, parse_pragma @@ -403,6 +404,12 @@ class FormatChecker(BaseTokenChecker, BaseRawFileChecker): # the full line; therefore we check the next token on the line. if tok_type == tokenize.INDENT: self.new_line(TokenWrapper(tokens), idx - 1, idx + 1) + # A tokenizer oddity: if a line contains a multi-line string, + # the NEWLINE also gets its own token which we already checked in + # the multi-line docstring case. + # See https://github.com/PyCQA/pylint/issues/6936 + elif tok_type == tokenize.NEWLINE: + pass else: self.new_line(TokenWrapper(tokens), idx - 1, idx) @@ -584,7 +591,10 @@ class FormatChecker(BaseTokenChecker, BaseRawFileChecker): stripped_line = line.rstrip("\t\n\r\v ") if line[len(stripped_line) :] not in ("\n", "\r\n"): self.add_message( - "trailing-whitespace", line=i, col_offset=len(stripped_line) + "trailing-whitespace", + line=i, + col_offset=len(stripped_line), + confidence=HIGH, ) def check_line_length(self, line: str, i: int, checker_off: bool) -> None: diff --git a/tests/functional/t/trailing_whitespaces.py b/tests/functional/t/trailing_whitespaces.py index 8deaca78d..cb9d642ee 100644 --- a/tests/functional/t/trailing_whitespaces.py +++ b/tests/functional/t/trailing_whitespaces.py @@ -1,10 +1,20 @@ """Regression test for trailing-whitespace (C0303).""" -# pylint: disable=mixed-line-endings +# pylint: disable=mixed-line-endings,pointless-string-statement # +1: [trailing-whitespace] print('some trailing whitespace') # +1: [trailing-whitespace] print('trailing whitespace does not count towards the line length limit') -print('windows line ends are ok')
-# +1: [trailing-whitespace]
-print('but trailing whitespace on win is not')
+print('windows line ends are ok') +# +1: [trailing-whitespace] +print('but trailing whitespace on win is not') + +# Regression test for https://github.com/PyCQA/pylint/issues/6936 +# +2: [trailing-whitespace] +""" This module has the Board class. +""" + +# +3: [trailing-whitespace] +""" This module has the Board class. +It's a very nice Board. +""" diff --git a/tests/functional/t/trailing_whitespaces.txt b/tests/functional/t/trailing_whitespaces.txt index 45990737f..913e29a5a 100644 --- a/tests/functional/t/trailing_whitespaces.txt +++ b/tests/functional/t/trailing_whitespaces.txt @@ -1,3 +1,5 @@ -trailing-whitespace:5:33:None:None::Trailing whitespace:UNDEFINED -trailing-whitespace:7:73:None:None::Trailing whitespace:UNDEFINED -trailing-whitespace:10:46:None:None::Trailing whitespace:UNDEFINED +trailing-whitespace:5:33:None:None::Trailing whitespace:HIGH +trailing-whitespace:7:73:None:None::Trailing whitespace:HIGH +trailing-whitespace:10:46:None:None::Trailing whitespace:HIGH +trailing-whitespace:15:3:None:None::Trailing whitespace:HIGH +trailing-whitespace:20:3:None:None::Trailing whitespace:HIGH |