Skip to content

Commit

Permalink
obtaining list of comment directives to ignore from pylintrc
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Fine authored and Pierre-Sassoulas committed Apr 10, 2021
1 parent 3c105a4 commit a9bb2ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
34 changes: 22 additions & 12 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pylint/testutils/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
21 changes: 21 additions & 0 deletions tests/checkers/unittest_spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a9bb2ad

Please sign in to comment.