Skip to content
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
9 changes: 5 additions & 4 deletions mypy_silent/maho.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import re
from typing import Optional


_type_ignore_re = re.compile(r"# type: ignore(\[[a-z, \-]+\])?")


def add_type_ignore_comment(line: str, error_code: Optional[str]) -> str:
# Workarounds for https://mypy.readthedocs.io/en/stable/common_issues.html#silencing-linters

Expand All @@ -22,9 +26,6 @@ def add_type_ignore_comment(line: str, error_code: Optional[str]) -> str:
def remove_type_ignore_comment(line: str) -> str:
content_without_crlf = line.rstrip("\r\n")
return (
content_without_crlf.replace("# type: ignore", "#")
.rstrip()
.rstrip("#")
.rstrip()
_type_ignore_re.sub("#", content_without_crlf).rstrip().rstrip("#").rstrip()
+ line[len(content_without_crlf) :]
)
2 changes: 2 additions & 0 deletions tests/test_maho.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def test_add_type_ignore_comment_with_error_code() -> None:
" host, port, protocol = m.groups()\r\n",
),
("# type: ignore. very good", "#. very good"),
("# type: ignore[misc]. very good", "#. very good"),
("# type: ignore[misc, arg-type]. very good", "#. very good"),
),
)
def test_remove_type_ignore_comment(input: str, output: str) -> None:
Expand Down