summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Fine <ubuntu@ip-172-31-89-59.ec2.internal>2021-04-09 17:27:53 +0000
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-04-10 08:36:29 +0200
commita9bb2ad5e8f00e960be7d7faa7794a8297752a82 (patch)
treedebd75904531e6903377117a355364f9cb296a9c
parent3c105a4c6f552082fbadfd3f535a1daaa2eaa973 (diff)
downloadpylint-git-a9bb2ad5e8f00e960be7d7faa7794a8297752a82.tar.gz
obtaining list of comment directives to ignore from pylintrc
-rw-r--r--pylint/checkers/spelling.py34
-rw-r--r--pylint/testutils/decorator.py10
-rw-r--r--tests/checkers/unittest_spelling.py21
3 files changed, 49 insertions, 16 deletions
diff --git a/pylint/checkers/spelling.py b/pylint/checkers/spelling.py
index bb2e30011..a1a9f6825 100644
--- a/pylint/checkers/spelling.py
+++ b/pylint/checkers/spelling.py
@@ -102,10 +102,13 @@ class WordsWithUnderscores(Filter):
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": "<comma separated words>",
+ "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 @@ class SpellingChecker(BaseTokenChecker):
# "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 @@ class SpellingChecker(BaseTokenChecker):
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 3fb5f190a..5e5077e7b 100644
--- a/pylint/testutils/decorator.py
+++ b/pylint/testutils/decorator.py
@@ -7,7 +7,11 @@ from pylint.testutils.checker_test_case import CheckerTestCase
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 set_config(**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 0d946bc84..d5011dba0 100644
--- a/tests/checkers/unittest_spelling.py
+++ b/tests/checkers/unittest_spelling.py
@@ -380,6 +380,27 @@ class TestSpellingChecker(
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):
stmt = astroid.extract_node(