Skip to content

Commit a9bb2ad

Browse files
Eli FinePierre-Sassoulas
Eli Fine
authored andcommitted
obtaining list of comment directives to ignore from pylintrc
1 parent 3c105a4 commit a9bb2ad

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

pylint/checkers/spelling.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@ def _skip(self, word):
102102

103103

104104
class RegExFilter(Filter):
105-
r"""Parent class for filters using regular expressions.
106-
This filter skips any words the match the expression assigned to the class attribute ``_pattern``
105+
"""Parent class for filters using regular expressions.
106+
107+
This filter skips any words the match the expression
108+
assigned to the class attribute ``_pattern``.
107109
108110
"""
111+
109112
_pattern: Pattern[str]
110113

111114
def _skip(self, word) -> bool:
@@ -269,6 +272,15 @@ class SpellingChecker(BaseTokenChecker):
269272
"help": "Limits count of emitted suggestions for spelling mistakes.",
270273
},
271274
),
275+
(
276+
"spelling-ignore-comment-directives",
277+
{
278+
"default": "fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy:",
279+
"type": "string",
280+
"metavar": "<comma separated words>",
281+
"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.",
282+
},
283+
),
272284
)
273285

274286
def open(self):
@@ -288,6 +300,10 @@ def open(self):
288300
# "pylint" appears in comments in pylint pragmas.
289301
self.ignore_list.extend(["param", "pylint"])
290302

303+
self.ignore_comment_directive_list = [
304+
w.strip() for w in self.config.spelling_ignore_comment_directives.split(",")
305+
]
306+
291307
# Expand tilde to allow e.g. spelling-private-dict-file = ~/.pylintdict
292308
if self.config.spelling_private_dict_file:
293309
self.config.spelling_private_dict_file = os.path.expanduser(
@@ -332,16 +348,10 @@ def _check_spelling(self, msgid, line, line_num):
332348
initial_space = 0
333349
if line.strip().startswith("#") and "docstring" not in msgid:
334350
line = line.strip()[1:]
335-
# 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.
336-
for iter_directive in (
337-
"fmt: on",
338-
"fmt: off",
339-
"noqa:",
340-
"noqa",
341-
"nosec",
342-
"isort:skip",
343-
"mypy:",
344-
):
351+
# A ``Filter`` cannot determine if the directive is at the beginning of a line,
352+
# nor determine if a colon is present or not (``pyenchant`` strips trailing colons).
353+
# So implementing this here.
354+
for iter_directive in self.ignore_comment_directive_list:
345355
if line.startswith(" " + iter_directive):
346356
line = line[(len(iter_directive) + 1) :]
347357
break

pylint/testutils/decorator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88

99
def set_config(**kwargs):
10-
"""Decorator for setting config values on a checker."""
10+
"""Decorator for setting config values on a checker.
11+
12+
Passing the args and kwargs back to the test function itself
13+
allows this decorator to be used on parametrized test cases.
14+
"""
1115

1216
def _wrapper(fun):
1317
@functools.wraps(fun)
@@ -17,9 +21,7 @@ def _forward(self, *args, **test_function_kwargs):
1721
if isinstance(self, CheckerTestCase):
1822
# reopen checker in case, it may be interested in configuration change
1923
self.checker.open()
20-
fun(
21-
self, *args, **test_function_kwargs
22-
) # Passing the args and kwargs back to the test function itself allows this decorator to be used on parametrized test cases
24+
fun(self, *args, **test_function_kwargs)
2325

2426
return _forward
2527

tests/checkers/unittest_spelling.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,27 @@ def test_skip_code_flanked_in_single_backticks(self):
379379
):
380380
self.checker.process_tokens(_tokenize_str(full_comment))
381381

382+
@skip_on_missing_package_or_dict
383+
@set_config(
384+
spelling_dict=spell_dict,
385+
spelling_ignore_comment_directives="newdirective:,noqa",
386+
)
387+
def test_skip_directives_specified_in_pylintrc(self):
388+
full_comment = "# newdirective: do this newdirective"
389+
with self.assertAddsMessages(
390+
Message(
391+
"wrong-spelling-in-comment",
392+
line=1,
393+
args=(
394+
"newdirective",
395+
full_comment,
396+
" ^^^^^^^^^^^^",
397+
self._get_msg_suggestions("newdirective"),
398+
),
399+
)
400+
):
401+
self.checker.process_tokens(_tokenize_str(full_comment))
402+
382403
@skip_on_missing_package_or_dict
383404
@set_config(spelling_dict=spell_dict)
384405
def test_handle_words_joined_by_forward_slash(self):

0 commit comments

Comments
 (0)