-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_issues.py
More file actions
54 lines (40 loc) · 1.51 KB
/
fix_issues.py
File metadata and controls
54 lines (40 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Script to automatically fix common issues."""
import re
from pathlib import Path
def add_missing_imports(file_path):
"""Add common missing imports."""
with open(file_path, "r") as f:
content = f.read()
# Check for missing typing imports
if "typing." not in content and any(
x in content for x in ["List", "Dict", "Optional", "Tuple", "Union", "Callable"]
):
content = (
"from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union\n" + content
)
# Check for missing numpy
if "np." in content and "import numpy" not in content:
content = "import numpy as np\n" + content
# Check for missing re
if "re." in content and "import re" not in content:
content = "import re\n" + content
# Check for missing time
if "time." in content and "import time" not in content:
content = "import time\n" + content
with open(file_path, "w") as f:
f.write(content)
def remove_trailing_whitespace(file_path):
"""Remove trailing whitespace from each line."""
with open(file_path, "r") as f:
lines = f.readlines()
lines = [line.rstrip() + "\n" for line in lines]
with open(file_path, "w") as f:
f.writelines(lines)
# Process all Python files
for py_file in Path(".").rglob("*.py"):
print(f"Processing {py_file}")
try:
add_missing_imports(py_file)
remove_trailing_whitespace(py_file)
except Exception as e:
print(f"Error processing {py_file}: {e}")