From a9bb2ad5e8f00e960be7d7faa7794a8297752a82 Mon Sep 17 00:00:00 2001 From: Eli Fine Date: Fri, 9 Apr 2021 17:27:53 +0000 Subject: [PATCH] obtaining list of comment directives to ignore from pylintrc --- pylint/checkers/spelling.py | 34 +++++++++++++++++++---------- pylint/testutils/decorator.py | 10 +++++---- tests/checkers/unittest_spelling.py | 21 ++++++++++++++++++ 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/pylint/checkers/spelling.py b/pylint/checkers/spelling.py index bb2e30011c..a1a9f6825b 100644 --- a/pylint/checkers/spelling.py +++ b/pylint/checkers/spelling.py @@ -102,10 +102,13 @@ def _skip(self, word): class RegExFilter(Filter): - r"""Parent class for filters using regular expressions. - This filter skips any words the match the expression assigned to the class attribute ``_pattern`` + """Parent class for filters using regular expressions. + + This filter skips any words the match the expression + assigned to the class attribute ``_pattern``. """ + _pattern: Pattern[str] def _skip(self, word) -> bool: @@ -269,6 +272,15 @@ class SpellingChecker(BaseTokenChecker): "help": "Limits count of emitted suggestions for spelling mistakes.", }, ), + ( + "spelling-ignore-comment-directives", + { + "default": "fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy:", + "type": "string", + "metavar": "", + "help": "List of comma separated words that should not be considered directives if they appear and the beginning of a comment and should not be checked.", + }, + ), ) def open(self): @@ -288,6 +300,10 @@ def open(self): # "pylint" appears in comments in pylint pragmas. self.ignore_list.extend(["param", "pylint"]) + self.ignore_comment_directive_list = [ + w.strip() for w in self.config.spelling_ignore_comment_directives.split(",") + ] + # Expand tilde to allow e.g. spelling-private-dict-file = ~/.pylintdict if self.config.spelling_private_dict_file: self.config.spelling_private_dict_file = os.path.expanduser( @@ -332,16 +348,10 @@ def _check_spelling(self, msgid, line, line_num): initial_space = 0 if line.strip().startswith("#") and "docstring" not in msgid: line = line.strip()[1:] - # A ``Filter`` cannot determine if the directive is at the beginning of a line, nor determine if a colon is present or not (``pyenchant`` strips trailing colons). So implementing this here. - for iter_directive in ( - "fmt: on", - "fmt: off", - "noqa:", - "noqa", - "nosec", - "isort:skip", - "mypy:", - ): + # A ``Filter`` cannot determine if the directive is at the beginning of a line, + # nor determine if a colon is present or not (``pyenchant`` strips trailing colons). + # So implementing this here. + for iter_directive in self.ignore_comment_directive_list: if line.startswith(" " + iter_directive): line = line[(len(iter_directive) + 1) :] break diff --git a/pylint/testutils/decorator.py b/pylint/testutils/decorator.py index 3fb5f190aa..5e5077e7bc 100644 --- a/pylint/testutils/decorator.py +++ b/pylint/testutils/decorator.py @@ -7,7 +7,11 @@ def set_config(**kwargs): - """Decorator for setting config values on a checker.""" + """Decorator for setting config values on a checker. + + Passing the args and kwargs back to the test function itself + allows this decorator to be used on parametrized test cases. + """ def _wrapper(fun): @functools.wraps(fun) @@ -17,9 +21,7 @@ def _forward(self, *args, **test_function_kwargs): if isinstance(self, CheckerTestCase): # reopen checker in case, it may be interested in configuration change self.checker.open() - fun( - self, *args, **test_function_kwargs - ) # Passing the args and kwargs back to the test function itself allows this decorator to be used on parametrized test cases + fun(self, *args, **test_function_kwargs) return _forward diff --git a/tests/checkers/unittest_spelling.py b/tests/checkers/unittest_spelling.py index 0d946bc847..d5011dba0e 100644 --- a/tests/checkers/unittest_spelling.py +++ b/tests/checkers/unittest_spelling.py @@ -379,6 +379,27 @@ def test_skip_code_flanked_in_single_backticks(self): ): self.checker.process_tokens(_tokenize_str(full_comment)) + @skip_on_missing_package_or_dict + @set_config( + spelling_dict=spell_dict, + spelling_ignore_comment_directives="newdirective:,noqa", + ) + def test_skip_directives_specified_in_pylintrc(self): + full_comment = "# newdirective: do this newdirective" + with self.assertAddsMessages( + Message( + "wrong-spelling-in-comment", + line=1, + args=( + "newdirective", + full_comment, + " ^^^^^^^^^^^^", + self._get_msg_suggestions("newdirective"), + ), + ) + ): + self.checker.process_tokens(_tokenize_str(full_comment)) + @skip_on_missing_package_or_dict @set_config(spelling_dict=spell_dict) def test_handle_words_joined_by_forward_slash(self):