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

[Backport maintenance/2.16.x] [spelling] Ignore spelling in type/mypy type ignore comments #8373

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 doc/whatsnew/fragments/8370.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix false positive for ``wrong-spelling-in-comment`` with class names in a python 2 type comment.

Closes #8370
23 changes: 4 additions & 19 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def _next(self) -> tuple[str, Literal[0]]:


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 @@ -190,23 +189,6 @@ def replace_code_but_leave_surrounding_characters(match_obj: re.Match[str]) -> s
)


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: re.Match[str],
) -> 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 @@ -362,7 +344,6 @@ def _check_spelling(self, msgid: str, line: str, line_num: int) -> None:
starts_with_comment = False

line = _strip_code_flanked_in_backticks(line)
line = _strip_mypy_ignore_directive_rule(line)

for word, word_start_at in self.tokenizer(line.strip()):
word_start_at += initial_space
Expand Down Expand Up @@ -436,6 +417,10 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
if token.startswith("# pylint:"):
# Skip pylint enable/disable comments
continue
if token.startswith("# type: "):
# Skip python 2 type comments and mypy type ignore comments
# mypy do not support additional text in type comments
continue
self._check_spelling("wrong-spelling-in-comment", token, start_row)

@only_required_for_messages("wrong-spelling-in-docstring")
Expand Down
39 changes: 21 additions & 18 deletions tests/checkers/unittest_spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ def test_skip_urls(self) -> None:
self.checker.process_tokens(_tokenize_str("# https://github.com/rfk/pyenchant"))
assert not self.linter.release_messages()

@skip_on_missing_package_or_dict
@set_config(spelling_dict=spell_dict)
@pytest.mark.parametrize(
"type_comment",
[
"# type: (NotAWord) -> NotAWord",
"# type: List[NotAWord] -> List[NotAWord]",
"# type: Dict[NotAWord] -> Dict[NotAWord]",
"# type: NotAWord",
"# type: List[NotAWord]",
"# type: Dict[NotAWord]",
"# type: ImmutableList[Manager]",
# will result in error: Invalid "type: ignore" comment [syntax]
# when analyzed with mypy 1.02
"# type: ignore[attr-defined] NotAWord",
],
)
def test_skip_type_comments(self, type_comment: str) -> None:
self.checker.process_tokens(_tokenize_str(type_comment))
assert not self.linter.release_messages()

@skip_on_missing_package_or_dict
@set_config(spelling_dict=spell_dict)
def test_skip_sphinx_directives(self) -> None:
Expand Down Expand Up @@ -348,24 +369,6 @@ def test_skip_code_flanked_in_single_backticks(self) -> None:
):
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) -> None:
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