summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Fine <eli88fine@gmail.com>2022-05-06 04:10:07 -0400
committerGitHub <noreply@github.com>2022-05-06 10:10:07 +0200
commitea4e69b5fd6a1b1dac366082a7bade88c4cfad80 (patch)
tree793dd2a2dd1b6a48e00306a5bf370afe1a623b6d
parentd6f636b675a8497f4ee64fbc15c8cc041a261d40 (diff)
downloadpylint-git-ea4e69b5fd6a1b1dac366082a7bade88c4cfad80.tar.gz
spellcheck will skip the rule names of mypy inline directives (#5929)
Co-authored-by: Eli Fine <eli.fine@resilience.com> Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r--ChangeLog3
-rw-r--r--doc/whatsnew/2.14.rst4
-rw-r--r--pylint/checkers/spelling.py17
-rw-r--r--tests/checkers/unittest_spelling.py20
4 files changed, 43 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index b903c1300..405d85f4e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1022,6 +1022,9 @@ Release date: 2022-03-24
Closes #4955
+* Disable spellchecking of mypy rule names in ignore directives.
+ Closes #5929
+
* Allow disabling ``duplicate-code`` with a disable comment when running through
pylint.
diff --git a/doc/whatsnew/2.14.rst b/doc/whatsnew/2.14.rst
index f1704dd62..bf97f475d 100644
--- a/doc/whatsnew/2.14.rst
+++ b/doc/whatsnew/2.14.rst
@@ -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
diff --git a/pylint/checkers/spelling.py b/pylint/checkers/spelling.py
index c159f9172..cfa24da81 100644
--- a/pylint/checkers/spelling.py
+++ b/pylint/checkers/spelling.py
@@ -161,6 +161,7 @@ class ForwardSlashChunker(Chunker):
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:
@@ -178,6 +179,21 @@ def _strip_code_flanked_in_backticks(line: str) -> 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."""
@@ -332,6 +348,7 @@ class SpellingChecker(BaseTokenChecker):
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
diff --git a/tests/checkers/unittest_spelling.py b/tests/checkers/unittest_spelling.py
index 9939ccb35..de1a95610 100644
--- a/tests/checkers/unittest_spelling.py
+++ b/tests/checkers/unittest_spelling.py
@@ -310,7 +310,7 @@ class TestSpellingChecker(CheckerTestCase): # pylint:disable=too-many-public-me
("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
@@ -374,6 +374,24 @@ class TestSpellingChecker(CheckerTestCase): # pylint:disable=too-many-public-me
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,
spelling_ignore_comment_directives="newdirective:,noqa",