-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
immutabilize.py
179 lines (151 loc) · 6.07 KB
/
immutabilize.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Loops through all TG classes and converts list-type attributes to tuples.
import inspect
import pickle
import re
from pathlib import Path
import isort
import telegram
changed_files = set()
processed_classes = set()
try:
processed_classes = pickle.load(open("processed_classes.pickle", "rb"))
except Exception:
print("Could not load pickle file")
processed_classes = set()
unprocessed_classes = set()
# loop through all classes in the `telegram` module
classes = inspect.getmembers(telegram, inspect.isclass)
for name, cls in classes:
if cls in processed_classes:
continue
print("Processing class", name)
# first adjust the __init__ of the class
params = inspect.signature(cls.__init__).parameters
params_to_change = {}
for param in params.values():
if "List" in str(param.annotation):
print(" Converting list-type parameter", param.name, "to Sequence")
params_to_change[param.name] = param.default
print(" ", param.name, "default value:", repr(param.default))
if not params_to_change:
continue
class_source_file = Path(inspect.getfile(cls))
if class_source_file not in changed_files:
changed_files.add(class_source_file)
processed_classes.add(cls)
else:
unprocessed_classes.add(cls)
continue
_, class_start_line = inspect.getsourcelines(cls)
class_source = inspect.getsource(cls)
class_source_lines = class_source.splitlines()
class_length = len(class_source_lines)
init_source_lines, init_start_line = inspect.getsourcelines(cls.__init__)
init_length = len(init_source_lines)
init_source = inspect.getsource(cls.__init__)
args_start_line = -1
attributes_start_line = -1
# Search "Args:" block in the docstring
for i, line in enumerate(class_source_lines):
if line.strip().startswith("Args:"):
args_start_line = i
if line.strip().startswith("Attributes:"):
attributes_start_line = i
if class_start_line + i == init_start_line:
break
# In the "Args:" block replace "List[" by "Sequence["
for i in range(args_start_line + 1, attributes_start_line):
whitespaces = -1
for param in params_to_change:
if f"{param} (List[" not in class_source_lines[i]:
continue
class_source_lines[i] = class_source_lines[i].replace(
f"{param} (List[", f"{param} (Sequence["
)
whitespaces = re.match(r" +", class_source_lines[i]).end() + 4
j = i + 1
while class_source_lines[j] and (
re.match(r"\s+", class_source_lines[j]).end() >= whitespaces
):
j = j + 1
class_source_lines[j - 1] += (
f"\n\n{whitespaces * ' '}.. versionchanged:: 20.0\n{whitespaces * ' '}"
" |squenceclassargs|"
)
if class_source_lines[j]:
class_source_lines[j - 1] += "\n"
# In the "Attributes:" block replace "List[" by "Sequence["
for i in range(attributes_start_line + 1, class_length):
whitespaces = -1
for param in params_to_change:
if f"{param} (List[" not in class_source_lines[i]:
continue
class_source_lines[i] = class_source_lines[i].replace(
f"{param} (List[", f"{param} (Tuple["
)
whitespaces = re.match(r" +", class_source_lines[i]).end() + 4
j = i + 1
while class_source_lines[j] and (
re.match(r"\s+", class_source_lines[j]).end() >= whitespaces
):
j = j + 1
class_source_lines[j - 1] += (
f"\n\n{whitespaces * ' '}.. versionchanged:: 20.0\n{whitespaces * ' '}"
" |tupleclassattrs|"
)
if class_source_lines[j]:
class_source_lines[j - 1] += "\n"
# Adjust type annotations in the __init__ and converts to tuples before assigning to
# attributes
for param, default_value in params_to_change.items():
init_source = init_source.replace(param + ": List", param + ": Sequence")
init_source = re.sub(
rf"{param}: Union\[List\[(\w+)\], Tuple\[\w+, \.\.\.\]\]",
rf"{param}: Sequence[\1]",
init_source,
)
init_source = re.sub(
rf"{param}: Union\[Tuple\[\w+, \.\.\.\], List\[(\w+)\]\]",
rf"{param}: Sequence[\1]",
init_source,
)
if default_value is None:
init_source = re.sub(
rf"self\.{param} = (\S*)\n",
rf"self.{param} = tuple(\1) if \1 else None\n",
init_source,
)
else:
init_source = re.sub(
rf"self\.{param} = (\S*)\n", rf"self.{param} = tuple(\1)\n", init_source
)
init_source = re.sub(
rf"self\.{param} = (.*) or \[\]\n",
rf"self.{param} = tuple(\1) if \1 else ()\n",
init_source,
)
file_contents = class_source_file.read_text(encoding="utf-8")
file_contents = file_contents.splitlines()
# Insert new __init__
file_contents[
init_start_line - 1 : init_start_line + init_length - 1
] = init_source.splitlines()
# Insert new docstring
file_contents[class_start_line - 1 : init_start_line - 2] = class_source_lines[
: init_start_line - class_start_line - 1
]
i = 0
while file_contents[i].startswith("#"):
i += 1
file_contents[i] += "\nfrom typing import Sequence"
file_contents = "\n".join(file_contents) + "\n"
# Sort imports
file_contents = isort.code(file_contents)
class_source_file.write_text(file_contents, encoding="utf-8")
if unprocessed_classes:
print(
"Rerun the script to finish the conversion. I can't handle files that contain multiple "
"classes. The following classes were not processed:"
f"{', '.join([cls.__name__ for cls in unprocessed_classes])}"
)
pickle.dump(processed_classes, open("processed_classes.pickle", "wb"))