-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_docstrings.py
364 lines (292 loc) · 9.93 KB
/
test_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""Tests for [the `parsers.docstrings` module][pytkdocs.parsers.docstrings]."""
import inspect
from textwrap import dedent
from pytkdocs.loader import Loader
from pytkdocs.parsers.docstrings import parse as _parse
def parse(docstring, signature=None, return_type=inspect.Signature.empty, admonitions=True):
return _parse("o", dedent(docstring).strip(), signature, return_type, admonitions)
class TestDocstringParser:
def test_simple_docstring(self):
sections, errors = parse("A simple docstring.")
assert len(sections) == 1
assert not errors
def test_multi_line_docstring(self):
sections, errors = parse(
"""
A somewhat longer docstring.
Blablablabla.
"""
)
assert len(sections) == 1
assert not errors
def test_sections_without_signature(self):
sections, errors = parse(
"""
Sections without signature.
Parameters:
void: SEGFAULT.
niet: SEGFAULT.
nada: SEGFAULT.
rien: SEGFAULT.
Exceptions:
GlobalError: when nothing works as expected.
Returns:
Itself.
"""
)
assert len(sections) == 4
assert len(errors) == 5 # missing annotations for params and return
for error in errors[:-1]:
assert "param" in error
assert "return" in errors[-1]
def test_property_docstring(self):
class_ = Loader().get_object_documentation("tests.fixtures.parsing.docstrings.NotDefinedYet")
prop = class_.attributes[0]
sections, errors = prop.docstring_sections, prop.docstring_errors
assert len(sections) == 2
assert not errors
def test_function_without_annotations(self):
def f(x, y):
"""
This function has no annotations.
Parameters:
x: X value.
y: Y value.
Returns:
Sum X + Y.
"""
return x + y
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 3
assert len(errors) == 1
assert "No type in return" in errors[0]
def test_function_with_annotations(self):
def f(x: int, y: int) -> int:
"""
This function has annotations.
Parameters:
x: X value.
y: Y value.
Returns:
Sum X + Y.
"""
return x + y
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 3
assert not errors
def test_types_in_docstring(self):
def f(x, y):
"""
The types are written in the docstring.
Parameters:
x (int): X value.
y (int): Y value.
Returns:
int: Sum X + Y.
"""
return x + y
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 3
assert not errors
x, y = sections[1].value
r = sections[2].value
assert x.name == "x"
assert x.annotation == "int"
assert x.description == "X value."
assert x.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
assert x.default is inspect.Signature.empty
assert y.name == "y"
assert y.annotation == "int"
assert y.description == "Y value."
assert y.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
assert y.default is inspect.Signature.empty
assert r.annotation == "int"
assert r.description == "Sum X + Y."
def test_types_and_optional_in_docstring(self):
def f(x=1, y=None):
"""
The types are written in the docstring.
Parameters:
x (int): X value.
y (int, optional): Y value.
Returns:
int: Sum X + Y.
"""
return x + (y or 1)
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 3
assert not errors
x, y = sections[1].value
assert x.name == "x"
assert x.annotation == "int"
assert x.description == "X value."
assert x.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
assert x.default == 1
assert y.name == "y"
assert y.annotation == "int"
assert y.description == "Y value."
assert y.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
assert y.default is None
def test_types_in_signature_and_docstring(self):
def f(x: int, y: int) -> int:
"""
The types are written both in the signature and in the docstring.
Parameters:
x (int): X value.
y (int): Y value.
Returns:
int: Sum X + Y.
"""
return x + y
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 3
assert not errors
def test_close_sections(self):
def f(x, y, z):
"""
Parameters:
x: X.
Parameters:
y: Y.
Parameters:
z: Z.
Exceptions:
Error2: error.
Exceptions:
Error1: error.
Returns:
1.
Returns:
2.
"""
return x + y + z
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 7
assert len(errors) == 2 # no return type annotations
def test_code_blocks(self):
def f(s):
"""
This docstring contains a docstring in a code block o_O!
```python
\"\"\"
This docstring is contained in another docstring O_o!
Parameters:
s: A string.
\"\"\"
```
"""
return s
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
assert not errors
def test_indented_code_block(self):
def f(s):
"""
This docstring contains a docstring in a code block o_O!
\"\"\"
This docstring is contained in another docstring O_o!
Parameters:
s: A string.
\"\"\"
"""
return s
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
assert not errors
def test_extra_parameter(self):
def f(x):
"""
Parameters:
x: Integer.
y: Integer.
"""
return x
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
assert len(errors) == 1
assert "No type" in errors[0]
def test_missing_parameter(self):
def f(x, y):
"""
Parameters:
x: Integer.
"""
return x + y
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
assert not errors
def test_param_line_without_colon(self):
def f(x: int):
"""
Parameters:
x is an integer.
"""
return x
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert not sections # getting x fails, so the section is empty and discarded
assert len(errors) == 2
assert "pair" in errors[0]
assert "Empty" in errors[1]
def test_admonitions(self):
def f():
"""
Note:
Hello.
Note: With title.
Hello again.
Something:
Something.
"""
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
assert not errors
def test_invalid_sections(self):
def f():
"""
Parameters:
Exceptions:
Exceptions:
Returns:
Note:
Important:
"""
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
for error in errors[:3]:
assert "Empty" in error
assert "No return type" in errors[3]
assert "Empty" in errors[-1]
def test_multiple_lines_in_sections_items(self):
def f(p: str, q: str):
"""
Hi.
Arguments:
p: This argument
has a description
spawning on multiple lines.
It even has blank lines in it.
Some of these lines
are indented for no reason.
q:
What if the first line is blank?
"""
return p + q
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 2
assert not errors
def test_parse_args_kwargs(self):
def f(a, *args, **kwargs):
"""
Arguments:
a: a parameter.
*args: args parameters.
**kwargs: kwargs parameters.
"""
return 1
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
assert len(sections) == 1
expected_parameters = {"a": "a parameter.", "*args": "args parameters.", "**kwargs": "kwargs parameters."}
for param in sections[0].value:
assert param.name in expected_parameters
assert expected_parameters[param.name] == param.description
assert not errors