-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix_annotations.py
More file actions
55 lines (46 loc) · 1.84 KB
/
Copy pathfix_annotations.py
File metadata and controls
55 lines (46 loc) · 1.84 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
55
import os, glob
for filepath in glob.glob('moira/**/*.py', recursive=True):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
if 'from __future__ import annotations' in content:
continue
lines = content.split('\n')
out_lines = []
in_docstring = False
docstring_char = None
inserted = False
for line in lines:
if inserted:
out_lines.append(line)
continue
stripped = line.strip()
if not in_docstring and (stripped.startswith('"""') or stripped.startswith("'''")):
out_lines.append(line)
in_docstring = True
docstring_char = stripped[:3]
# Handle single-line docstring
if len(stripped) >= 6 and stripped.endswith(docstring_char):
in_docstring = False
out_lines.append("\nfrom __future__ import annotations")
inserted = True
elif in_docstring:
out_lines.append(line)
if stripped.endswith(docstring_char):
in_docstring = False
out_lines.append("\nfrom __future__ import annotations")
inserted = True
else:
# We are not in a docstring
if stripped == '' or stripped.startswith('#'):
out_lines.append(line)
else:
# Actual code starts here
out_lines.append("from __future__ import annotations\n")
out_lines.append(line)
inserted = True
# If file was completely empty or only comments
if not inserted:
out_lines.insert(0, "from __future__ import annotations\n")
with open(filepath, 'w', encoding='utf-8') as f:
f.write('\n'.join(out_lines))
print("Fixed missing annotations.")