forked from pytorch/pytorch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to codemod in 'type: ignore' comments
ghstack-source-id: f5b12cc77795af5c9a2991680634fb7b6b2d3dfe Pull Request resolved: pytorch#57994
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser(description='Add type ignore comments to files automatically.') | ||
parser.add_argument('--log', help='mypy log file (mypy 2>&1 > log)') | ||
args = parser.parse_args() | ||
|
||
with open(args.log, "r") as f: | ||
for line in f.readlines(): | ||
if "error:" not in line: | ||
# Summary or note line, ignore it | ||
continue | ||
line = line.strip() | ||
filename = line.split(":")[0] | ||
lineno = int(line.split(":")[1]) | ||
error_code = line.split(" ")[-1].replace("[", "").replace("]", "") | ||
|
||
content = open(filename, "r").readlines() | ||
output = [] | ||
for index, content_line in enumerate(content): | ||
if content_line[-1] == "\n": | ||
# remove trailing newline | ||
content_line = content_line[:-1] | ||
|
||
if index + 1 == lineno: | ||
if content_line.strip().endswith("\\"): | ||
print(f"Fix {filename}:{lineno} - add # type: ignore[{error_code}]") | ||
elif "type: ignore" not in content_line: | ||
content_line = content_line + f" # type: ignore[{error_code}]" | ||
else: | ||
# add to existing type ignore | ||
content_line = content_line[:-1] + f", {error_code}]" | ||
output.append(content_line) | ||
|
||
open(filename, "w").write("\n".join(output) + "\n") |