Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spellcheck will skip the rule names of mypy inline directives #5929

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,9 @@ Release date: 2022-03-24

Closes #4955

* Disable spellchecking of mypy rule names in ignore directives.
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
Closes #5929

ejfine marked this conversation as resolved.
Show resolved Hide resolved
* Allow disabling ``duplicate-code`` with a disable comment when running through
pylint.

Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ Other Changes

Closes #6372

* Disable spellchecking of mypy rule names in ignore directives.

Closes #5929

* ``implicit-str-concat`` will now be raised on calls like ``open("myfile.txt" "a+b")`` too.

Closes #6441
Expand Down
17 changes: 17 additions & 0 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def _next(self):


CODE_FLANKED_IN_BACKTICK_REGEX = re.compile(r"(\s|^)(`{1,2})([^`]+)(\2)([^`]|$)")
MYPY_IGNORE_DIRECTIVE_RULE_REGEX = re.compile(r"(\s|^)(type\: ignore\[[^\]]+\])(.*)")


def _strip_code_flanked_in_backticks(line: str) -> str:
Expand All @@ -178,6 +179,21 @@ def replace_code_but_leave_surrounding_characters(match_obj) -> str:
)


def _strip_mypy_ignore_directive_rule(line: str) -> str:
"""Alter line so mypy rule name is ignored.

Pyenchant parses anything flanked by spaces as an individual token,
so this cannot be done at the individual filter level.
"""

def replace_rule_name_but_leave_surrounding_characters(match_obj) -> str:
return match_obj.group(1) + match_obj.group(3)

return MYPY_IGNORE_DIRECTIVE_RULE_REGEX.sub(
replace_rule_name_but_leave_surrounding_characters, line
)


class SpellingChecker(BaseTokenChecker):
"""Check spelling in comments and docstrings."""

Expand Down Expand Up @@ -332,6 +348,7 @@ def _check_spelling(self, msgid: str, line: str, line_num: int) -> None:
starts_with_comment = False

line = _strip_code_flanked_in_backticks(line)
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
line = _strip_mypy_ignore_directive_rule(line)

for word, word_start_at in self.tokenizer(line.strip()):
word_start_at += initial_space
Expand Down
20 changes: 19 additions & 1 deletion tests/checkers/unittest_spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def test_skip_sphinx_directives_2(self):
("noqa", ":", "flake8 / zimports directive"),
("nosec", "", "bandit directive"),
("isort", ":skip", "isort directive"),
("mypy", ":", "mypy directive"),
("mypy", ":", "mypy top of file directive"),
),
)
def test_skip_tool_directives_at_beginning_of_comments_but_still_raise_error_if_directive_appears_later_in_comment( # pylint:disable=unused-argument
Expand Down Expand Up @@ -373,6 +373,24 @@ 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)
def test_skip_mypy_ignore_directives(self):
full_comment = "# type: ignore[attr-defined] attr"
with self.assertAddsMessages(
MessageTest(
"wrong-spelling-in-comment",
line=1,
args=(
"attr",
full_comment,
" ^^^^",
self._get_msg_suggestions("attr"),
),
)
):
self.checker.process_tokens(_tokenize_str(full_comment))

@skip_on_missing_package_or_dict
@set_config(
spelling_dict=spell_dict,
Expand Down