-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_docstrings.py
280 lines (244 loc) · 7.97 KB
/
check_docstrings.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import ast
import re
import importlib
import inspect
from dataclasses import dataclass
PATTERN_DIRECTIVE = re.compile(
r"^(?P<indentation> *)\.\. (?P<type>\w+):: (?P<name>\w+)(?P<signature>\(.+\)(?: -> .+)?)?",
flags=re.MULTILINE,
)
PATTERN_SIGNATURE = re.compile(
r"^(?P<indentation> *)def (?P<name>\w+)(?P<signature>\(.+\)(?: -> .+)?):",
flags=re.MULTILINE,
)
PATTERN_RST_PREFIX = re.compile(r":\w+:`")
PATTERN_SHORT_REFERENCE = re.compile(r"~(\w+\.)+")
PATTERN_SIMPLIFY_DOCSTRING = re.compile(r"[\W\s]")
PATTERN_MAGIC_METHOD = re.compile(r"\w+\.__[a-zA-Z0-9]+__")
@dataclass
class Item:
indentation: int | None
type: str | None
name: str
signature: str | None
start: int | None
doc_start: int | None
doc_end: int | None
docstring: str | None
def get_reference_items():
with open("doc/source/api_reference.rst") as f:
reference = f.read()
items = []
for match in PATTERN_DIRECTIVE.finditer(reference):
items.append(Item(
indentation=len(match.group("indentation")),
type=match.group("type"),
name=match.group("name"),
signature=match.group("signature"),
start=match.start(),
doc_start=match.end(),
doc_end=None,
docstring=None,
))
# use start of next item as doc_end
for i1, i2 in zip(items, items[1:]):
i1.doc_end = i2.start
# last item has docstring until end of text
items[-1].doc_end = len(reference)
# add docstrings, fix names of methods/attributes
current_class = None
for item in items:
# TODO should check minimum indentation level in docstring
item.docstring = reference[item.doc_start:item.doc_end]
if item.type == "class":
current_class = item.name
elif item.type in ("attribute", "method", "classmethod"):
if current_class is None:
raise SyntaxError(f"{item} must be part of a class")
item.name = f"{current_class}.{item.name}"
else:
current_class = None
return {item.name: item for item in items}
def make_stub_item_assign(prefix, node, docstring):
return Item(
indentation=None,
type=None,
name=prefix + node.targets[0].id,
signature=None,
start=None,
doc_start=None,
doc_end=None,
docstring=docstring,
)
def make_stub_item(prefix, node):
signature = None
try:
code = ast.unparse(node)
match = PATTERN_SIGNATURE.search(code)
signature = match.group("signature")
except AttributeError:
pass
docstring = None
try:
docstring = ast.get_docstring(node)
except TypeError:
docstring = None
return Item(
indentation=None,
type=None,
name=prefix + node.name,
signature=signature,
start=None,
doc_start=None,
doc_end=None,
docstring=docstring,
)
def get_stub_items():
with open("ducer/_fst.pyi", 'r') as f:
pyi_content = f.read()
items = []
tree = ast.parse(pyi_content)
todo = [("", child) for child in ast.iter_child_nodes(tree)]
previous_value = None
while todo:
prefix, node = todo.pop()
if prefix == "Op.":
pass
# remember orphaned values
if isinstance(node, ast.Expr):
previous_value = node.value.value
# handle class variables
if isinstance(node, ast.Assign):
items.append(make_stub_item_assign(prefix, node, previous_value))
previous_value = None
continue
if not hasattr(node, "name"):
continue
try:
items.append(make_stub_item(prefix, node))
except TypeError:
continue
prefix += node.name + "."
if isinstance(node, (ast.ClassDef)):
todo.extend((prefix, child) for child in ast.iter_child_nodes(node))
return {item.name: item for item in items}
def make_module_item(prefix, obj):
signature = None
try:
signature = str(inspect.signature(obj))
except ValueError:
pass
return Item(
indentation=None,
type=None,
name=prefix + obj.__name__,
signature=signature,
start=None,
doc_start=None,
doc_end=None,
docstring=inspect.getdoc(obj),
)
def get_module_items():
module = importlib.import_module("ducer._fst")
items = []
for top_name, obj in inspect.getmembers(module):
try:
items.append(make_module_item("", obj))
except TypeError:
pass
if inspect.isclass(obj):
prefix = top_name + "."
for _, obj in inspect.getmembers(obj):
try:
items.append(make_module_item(prefix, obj))
except TypeError:
pass
return {item.name: item for item in items}
def simplify_docstring(docstring):
docstring, _ = PATTERN_SHORT_REFERENCE.subn("", docstring)
docstring, _ = PATTERN_RST_PREFIX.subn("", docstring)
docstring, _ = PATTERN_SIMPLIFY_DOCSTRING.subn("", docstring)
docstring = docstring.lower()
return docstring
def docstrings_differ(item1, item2):
d1 = item1.docstring
d2 = item2.docstring
if d1 is None or d2 is None:
return d1 != d2
d1 = simplify_docstring(d1)
d2 = simplify_docstring(d2)
return d1 != d2
def simplify_signature(signature):
if signature is None:
return None
signature, _ = PATTERN_SHORT_REFERENCE.subn("", signature)
return signature
def signatures_differ(item1, item2):
s1 = simplify_signature(item1.signature)
s2 = simplify_signature(item2.signature)
return s1 != s2
def is_magic(name):
return PATTERN_MAGIC_METHOD.search(name) is not None
def main():
reference = get_reference_items()
stub = get_stub_items()
module = get_module_items()
for name, item in reference.items():
if name not in stub:
print()
print()
print("#======================================")
print(f"#{name} missing in stub!")
continue
other = stub[name]
if docstrings_differ(item, other):
print()
print()
print("#======================================")
print(f"#{name}: reference and stub docstrings differ")
print()
print("#reference")
print(item.docstring)
print()
print("#stub")
print(other.docstring)
# TODO fix signature comparison for classes
# e.g., reference is `(data: SupportsBytes)`,
# stub is `(self, data: SupportsBytes)`
if item.type != "class" and signatures_differ(item, other):
print()
print()
print("======================================")
print(f"{name}: reference and stub signatures differ")
print()
print("reference")
print(item.signature)
print()
print("stub")
print(other.signature)
for name, item in stub.items():
# workaround: don't check magic methods, Rust docstrings are not considered
if is_magic(name):
continue
if name not in module:
# workaround: attributes can't have docstrings
if not name.startswith("Op."):
print()
print()
print("#======================================")
print(f"#{name} missing in module!")
continue
other = module[name]
if docstrings_differ(item, other):
print()
print()
print("#======================================")
print(f"#{name}: stub and module docstrings differ")
print()
print("#stub")
print(item.docstring)
print()
print("#module")
print(other.docstring)
if __name__ == "__main__":
main()