From 0ab897742cdd9b6c81da3be5018ee0d29e74457c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 5 Feb 2019 08:43:24 +0100 Subject: Still use a regular expression for the FIXME check We need to be able to match multiple words (e.g. fixme and todo) but we should not catch strings such as `TODOIST`. The previous implementation was using a `startswith` between the notes and the string tokens but that matches Todoist as well. Close #2707 --- pylint/checkers/misc.py | 25 +++++++++++++++++-------- pylint/test/unittest_checker_misc.py | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/pylint/checkers/misc.py b/pylint/checkers/misc.py index 6c17918f6..a06b9a181 100644 --- a/pylint/checkers/misc.py +++ b/pylint/checkers/misc.py @@ -22,6 +22,8 @@ import tokenize +import re + from pylint.interfaces import IRawChecker, ITokenChecker from pylint.checkers import BaseChecker from pylint.utils import OPTION_RGX, MessagesHandlerMixIn @@ -103,6 +105,12 @@ class EncodingChecker(BaseChecker): ), ) + def open(self): + super().open() + self._fixme_pattern = re.compile( + r"#\s*(%s)\b" % "|".join(map(re.escape, self.config.notes)), re.I + ) + def _check_encoding(self, lineno, line, file_encoding): try: return line.decode(file_encoding) @@ -157,14 +165,15 @@ class EncodingChecker(BaseChecker): continue # emit warnings if necessary - for note in self.config.notes: - if comment_text.lower().startswith(note.lower()): - self.add_message( - "fixme", - args=comment_text, - line=comment.start[0], - col_offset=comment.string.lower().index(note.lower()), - ) + match = self._fixme_pattern.search("#" + comment_text.lower()) + if match: + note = match.group(1) + self.add_message( + "fixme", + col_offset=comment.string.lower().index(note.lower()), + args=comment_text, + line=comment.start[0], + ) def register(linter): diff --git a/pylint/test/unittest_checker_misc.py b/pylint/test/unittest_checker_misc.py index 3dce3d721..3fe49628d 100644 --- a/pylint/test/unittest_checker_misc.py +++ b/pylint/test/unittest_checker_misc.py @@ -86,3 +86,18 @@ class TestFixme(CheckerTestCase): Message(msg_id="fixme", line=1, args="TODO this should not trigger a fixme") ): self.checker.process_tokens(_tokenize_str(code)) + + def test_dont_trigger_on_todoist(self): + code = """ + # Todoist API: What is this task about? + # Todoist API: Look up a task's due date + # Todoist API: Look up a Project/Label/Task ID + # Todoist API: Fetch all labels + # Todoist API: "Name" value + # Todoist API: Get a task's priority + # Todoist API: Look up the Project ID a Task belongs to + # Todoist API: Fetch all Projects + # Todoist API: Fetch all Tasks + """ + with self.assertNoMessages(): + self.checker.process_tokens(_tokenize_str(code)) -- cgit v1.2.1