Skip to content

Instantly share code, notes, and snippets.

@inderpreet99
Created August 23, 2021 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inderpreet99/895f46392d839d7210f16d6a4083b803 to your computer and use it in GitHub Desktop.
Save inderpreet99/895f46392d839d7210f16d6a4083b803 to your computer and use it in GitHub Desktop.
Ignore MyPy errors by adding ignore comments using sed
import re
import subprocess # nosec
from typing import Callable, List
def uniq(arr: List[str]):
d = {}
for item in arr:
d[f"{item[0]}:{item[1]}"] = item[2]
return d
def cut(item: str, slicer: Callable, delim=":"):
filename = item.split(delim)[slicer] # type: ignore[call-overload]
try:
ignore_type = re.findall(" [(.*?)]", item)[-1]
except Exception:
print(("ignoring", item))
return None
return filename + [ignore_type]
def run():
output = subprocess.run(["mypy", "."], capture_output=True) # nosec
remove_last = output.stdout.splitlines()[:-1]
file_refs = [s.decode("utf-8") for s in remove_last]
files = [
cut(item, slice(0, 2))
for item in file_refs
if cut(item, slice(0, 2)) is not None
]
uniques = uniq(files)
for filename, ignore_type in uniques.items():
(file, line, *_) = filename.split(":")
subprocess.run( # nosec
[
"sed",
"-i",
f"{line},{line} {{s/$/ # type: ignore[{ignore_type}]/}}",
file,
]
) # no backups
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment