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

Add support for removing unused type ignores with error codes #42

Merged
Merged
Changes from 1 commit
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
Next Next commit
Add support for removing unused type ignores with error codes
patrick91 committed Apr 6, 2022

Unverified

This user has not yet uploaded their public signing key.
commit 0ba93e33ef5ad0d643bd79a465261ab048acde1b
9 changes: 8 additions & 1 deletion mypy_silent/maho.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
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

@@ -22,7 +29,7 @@ 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", "#")
_type_ignore_re.sub("#", content_without_crlf)
.rstrip()
.rstrip("#")
.rstrip()
2 changes: 2 additions & 0 deletions tests/test_maho.py
Original file line number Diff line number Diff line change
@@ -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: