Created
February 9, 2023 10:36
-
-
Save lixxday/280becb6cc9ea9eeef5013db94102304 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# First, run `mypy --show-error-codes --config-file mypy.ini src > mypy_results.txt` | |
def remove_unused_type_ignore(): | |
result_dict = {} | |
with open( | |
os.path.abspath(os.getcwd()) + "/mypy_results.txt", "r", encoding="utf8" | |
) as file_pointer: | |
lines = file_pointer.readlines() | |
lines.pop() | |
for line in lines: | |
split_line = line.split(" ") | |
filename_and_line = split_line[0] | |
log_message = split_line[2] | |
if 'Unused' not in log_message: | |
continue | |
try: | |
[filename, line_number, _] = filename_and_line.split(":") | |
except ValueError: | |
print("Error: Could not parse line: " + filename_and_line) | |
continue | |
if filename not in result_dict: | |
result_dict[filename] = [] | |
result_dict[filename].append(line_number) | |
for filename, line_number in result_dict.items(): | |
with open( | |
os.path.abspath(os.getcwd()) + "/" + filename, "r", encoding="utf8" | |
) as file_pointer: | |
lines = file_pointer.readlines() | |
with open( | |
os.path.abspath(os.getcwd()) + "/" + filename, "w", encoding="utf8" | |
) as file_pointer: | |
for line_number in line_number: | |
line = lines[int(line_number) - 1] | |
if "# type: ignore" not in line: | |
print("Error: Could not find type ignore comment in line: " + line) | |
else: | |
existing_comment = line.split("#")[-1].strip() | |
line = line.replace(f"# {existing_comment}", "") | |
lines[int(line_number) - 1] = line | |
file_pointer.writelines(lines) | |
remove_unused_type_ignore() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment