Repository navigation
Expand file tree
/
Copy pathfix_all_type_hints.py
More file actions
85 lines (67 loc) · 3.15 KB
/
Copy pathfix_all_type_hints.py
File metadata and controls
85 lines (67 loc) · 3.15 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""Comprehensive fix for Python 3.9 compatibility."""
import re
from pathlib import Path
def fix_file(filepath: Path) -> bool:
"""Fix type hints in a single file."""
content = filepath.read_text()
original = content
# Ensure Optional is imported
if 'Optional[' in content or (': ' in content and ' | None' in content):
if 'from typing import' in content:
if 'Optional' not in content.split('from typing import')[1].split('\n')[0]:
content = re.sub(
r'from typing import ([^\n]+)',
lambda m: f"from typing import {m.group(1)}, Optional" if 'Optional' not in m.group(1) else m.group(0),
content,
count=1
)
# Ensure Union is imported if needed
if 'Union[' in content or ' | ' in content:
if 'from typing import' in content:
if 'Union' not in content.split('from typing import')[1].split('\n')[0]:
content = re.sub(
r'from typing import ([^\n]+)',
lambda m: f"from typing import {m.group(1)}, Union" if 'Union' not in m.group(1) else m.group(0),
content,
count=1
)
# Fix Union[X, Y] | None -> Optional[Union[X, Y]]
content = re.sub(r'Union\[([^\]]+)\] \| None', r'Optional[Union[\1]]', content)
# Fix X | None -> Optional[X]
content = re.sub(r': (\w+) \| None', r': Optional[\1]', content)
content = re.sub(r'-> (\w+) \| None', r'-> Optional[\1]', content)
# Fix dict[...] | None -> Optional[dict[...]]
content = re.sub(r': (dict\[[^\]]+\]) \| None', r': Optional[\1]', content)
content = re.sub(r'-> (dict\[[^\]]+\]) \| None', r'-> Optional[\1]', content)
# Fix list[...] | None -> Optional[list[...]]
content = re.sub(r': (list\[[^\]]+\]) \| None', r': Optional[\1]', content)
content = re.sub(r'-> (list\[[^\]]+\]) \| None', r'-> Optional[\1]', content)
# Fix str | Path -> Union[str, Path]
content = re.sub(r': str \| Path', r': Union[str, Path]', content)
content = re.sub(r'-> str \| Path', r'-> Union[str, Path]', content)
# Fix Union[str, Path] | None -> Optional[Union[str, Path]]
content = re.sub(r'Union\[str, Path\] \| None', r'Optional[Union[str, Path]]', content)
# Fix dict | None -> Optional[dict]
content = re.sub(r': dict \| None', r': Optional[dict]', content)
content = re.sub(r'-> dict \| None', r'-> Optional[dict]', content)
if content != original:
filepath.write_text(content)
print(f"Fixed: {filepath}")
return True
return False
def main():
"""Fix all Python files."""
fixed_count = 0
for filepath in Path('agentcoach').rglob('*.py'):
if fix_file(filepath):
fixed_count += 1
for filepath in Path('tests').rglob('*.py'):
if fix_file(filepath):
fixed_count += 1
for filepath in Path('examples').rglob('*.py'):
if fix_file(filepath):
fixed_count += 1
print(f"\nFixed {fixed_count} files")
if __name__ == '__main__':
main()