Repository navigation
Expand file tree
/
Copy pathdocstrings_test.py
More file actions
544 lines (467 loc) · 21.8 KB
/
Copy pathdocstrings_test.py
File metadata and controls
544 lines (467 loc) · 21.8 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# docstrings_test.py - test for undocumented arguments
# RMM, 28 Jul 2024
#
# This unit test looks through all functions in the package and attempts to
# identify arguments that are not documented. It will check anything that
# is an explicitly listed argument, as well as attempt to find keyword
# arguments that are extracted using kwargs.pop(), config._get_param(), or
# config.use_legacy_defaults.
import inspect
import warnings
import pytest
import re
import control
import control.flatsys
# List of functions that we can skip testing (special cases)
function_skiplist = [
control.ControlPlot.reshape, # needed for legacy interface
control.phase_plot, # legacy function
control.drss, # documention in rss
control.frd, # tested separately below
control.ss, # tested separately below
control.tf, # tested separately below
]
# Checksums to use for checking whether a docstring has changed
function_docstring_hash = {
control.append: '1bddbac0fe932755c85e9fb0bfb97d88',
control.describing_function_plot: '95f894706b1d3eeb3b854934596af09f',
control.dlqe: '9db995ed95c2214ce97074b0616a3191',
control.dlqr: '896cfa651dbbd80e417635904d13c9d6',
control.lqe: '567bf657538935173f2e50700ba87168',
control.lqr: 'a3e0a85f781fc9c0f69a4b7da4f0bd22',
control.margin: 'f02b3034f5f1d44ce26f916cc3e51600',
control.parallel: 'bfc470aef75dbb923f9c6fb8bf3c9b43',
control.series: '9aede1459667738f05cf4fc46603a4f6',
control.ss2tf: 'e779b8d70205bc1218cc2a4556a66e4b',
control.tf2ss: '086a3692659b7321c2af126f79f4bc11',
control.markov: 'a4199c54cb50f07c0163d3790739eafe',
control.gangof4: '0e52eb6cf7ce024f9a41f3ae3ebf04f7',
}
# List of keywords that we can skip testing (special cases)
keyword_skiplist = {
control.input_output_response: ['method'],
control.nyquist_plot: ['color'], # separate check
control.optimal.solve_ocp: ['method', 'return_x'], # deprecated
control.sisotool: ['kvect'], # deprecated
control.nyquist_response: ['return_contour'], # deprecated
control.create_estimator_iosystem: ['state_labels'], # deprecated
control.bode_plot: ['sharex', 'sharey', 'margin_info'], # deprecated
control.eigensys_realization: ['arg'], # quasi-positional
control.find_operating_point: ['method'], # internal use
control.zpk: ['args'] # 'dt' (manual)
}
# Decide on the level of verbosity (use -rP when running pytest)
verbose = 1
@pytest.mark.parametrize("module, prefix", [
(control, ""), (control.flatsys, "flatsys."),
(control.optimal, "optimal."), (control.phaseplot, "phaseplot.")
])
def test_parameter_docs(module, prefix):
checked = set() # Keep track of functions we have checked
# Look through every object in the package
if verbose > 1:
print(f"Checking module {module}")
for name, obj in inspect.getmembers(module):
# Skip anything that is outside of this module
if inspect.getmodule(obj) is not None and (
not inspect.getmodule(obj).__name__.startswith('control')
or prefix != "" and inspect.getmodule(obj) != module):
# Skip anything that isn't part of the control package
continue
if inspect.isclass(obj):
if verbose > 1:
print(f" Checking class {name}")
# Check member functions within the class
test_parameter_docs(obj, prefix + name + '.')
if inspect.isfunction(obj):
# Skip anything that is inherited, hidden, deprecated, or checked
if inspect.isclass(module) and name not in module.__dict__ \
or name.startswith('_') or obj in function_skiplist or \
obj in checked:
continue
else:
checked.add(obj)
# Get the docstring (skip w/ warning if there isn't one)
if verbose > 1:
print(f" Checking function {name}")
if obj.__doc__ is None:
warnings.warn(
f"{module.__name__}.{name} is missing docstring")
continue
else:
docstring = inspect.getdoc(obj)
source = inspect.getsource(obj)
# Skip deprecated functions
if ".. deprecated::" in docstring:
if verbose > 1:
print(" [deprecated]")
continue
elif re.search(name + r"(\(\))? is deprecated", docstring) or \
"function is deprecated" in docstring:
if verbose > 1:
print(" [deprecated, but not numpydoc compliant]")
elif verbose:
print(f" {name} deprecation is not numpydoc compliant")
warnings.warn(f"{name} deprecated, but not numpydoc compliant")
continue
elif re.search(name + r"(\(\))? is deprecated", source):
if verbose:
print(f" {name} is deprecated, but not documented")
warnings.warn(f"{name} deprecated, but not documented")
continue
# Get the signature for the function
sig = inspect.signature(obj)
# Go through each parameter and make sure it is in the docstring
for argname, par in sig.parameters.items():
# Look for arguments that we can skip
if argname == 'self' or argname[0] == '_' or \
obj in keyword_skiplist and argname in keyword_skiplist[obj]:
continue
# Check for positional arguments
if par.kind == inspect.Parameter.VAR_POSITIONAL:
if obj in function_docstring_hash:
import hashlib
hash = hashlib.md5(
(docstring + source).encode('utf-8')).hexdigest()
if function_docstring_hash[obj] != hash:
pytest.fail(
f"source/docstring for {name}() modified; "
f"recheck docstring and update hash to "
f"{hash=}")
continue
# Too complicated to check
if f"*{argname}" not in docstring:
if verbose:
print(f" {name} has positional arguments; "
"check manually")
warnings.warn(
f"{name} {argname} has positional arguments; "
"docstring not checked")
continue
# Check for keyword arguments (then look at code for parsing)
elif par.kind == inspect.Parameter.VAR_KEYWORD:
# See if we documented the keyward argumnt directly
# if f"**{argname} :" in docstring:
# continue
# Look for direct kwargs argument access
kwargnames = set()
for _, kwargname in re.findall(
argname + r"(\[|\.pop\(|\.get\()'([\w]+)'",
source):
if verbose > 2:
print(" Found direct keyword argument",
kwargname)
kwargnames.add(kwargname)
# Look for kwargs accessed via _get_param
for kwargname in re.findall(
r"_get_param\(\s*'\w*',\s*'([\w]+)',\s*" + argname,
source):
if verbose > 2:
print(" Found config keyword argument",
{kwargname})
kwargnames.add(kwargname)
# Look for kwargs accessed via _process_legacy_keyword
for kwargname in re.findall(
r"_process_legacy_keyword\([\s]*" + argname +
r",[\s]*'[\w]+',[\s]*'([\w]+)'", source):
if verbose > 2:
print(" Found legacy keyword argument",
{kwargname})
kwargnames.add(kwargname)
for kwargname in kwargnames:
if obj in keyword_skiplist and \
kwargname in keyword_skiplist[obj]:
continue
if verbose > 3:
print(f" Checking keyword argument {kwargname}")
_check_parameter_docs(
name, kwargname, inspect.getdoc(obj),
prefix=prefix)
# Make sure this argument is documented properly in docstring
else:
if verbose > 3:
print(f" Checking argument {argname}")
_check_parameter_docs(
name, argname, docstring, prefix=prefix)
@pytest.mark.parametrize("module, prefix", [
(control, ""), (control.flatsys, "flatsys."),
(control.optimal, "optimal."), (control.phaseplot, "phaseplot.")
])
def test_deprecated_functions(module, prefix):
checked = set() # Keep track of functions we have checked
# Look through every object in the package
for name, obj in inspect.getmembers(module):
# Skip anything that is outside of this module
if inspect.getmodule(obj) is not None and (
not inspect.getmodule(obj).__name__.startswith('control')
or prefix != "" and inspect.getmodule(obj) != module):
# Skip anything that isn't part of the control package
continue
if inspect.isclass(obj):
# Check member functions within the class
test_deprecated_functions(obj, prefix + name + '.')
if inspect.isfunction(obj):
# Skip anything that is inherited, hidden, or checked
if inspect.isclass(module) and name not in module.__dict__ \
or name[0] == '_' or obj in checked:
continue
else:
checked.add(obj)
# Get the docstring (skip w/ warning if there isn't one)
if obj.__doc__ is None:
warnings.warn(
f"{module.__name__}.{name} is missing docstring")
continue
else:
docstring = inspect.getdoc(obj)
source = inspect.getsource(obj)
# Look for functions marked as deprecated in doc string
if ".. deprecated::" in docstring:
# Make sure a FutureWarning is issued
if not re.search("FutureWarning", source):
pytest.fail(
f"{name} deprecated but does not issue FutureWarning")
else:
if re.search(name + r"(\(\))? is deprecated", docstring) or \
re.search(name + r"(\(\))? is deprecated", source):
pytest.fail(
f"{name} deprecated but w/ non-standard docs/warnings")
assert name != 'ss2io'
#
# Tests for I/O system classes
#
# The tests below try to make sure that we document I/O system classes
# and the factory functions that create them in a uniform way.
#
ct = control
fs = control.flatsys
# Dictionary of factory functions associated with primary classes
class_factory_function = {
fs.FlatSystem: fs.flatsys,
ct.FrequencyResponseData: ct.frd,
ct.InterconnectedSystem: ct.interconnect,
ct.LinearICSystem: ct.interconnect,
ct.NonlinearIOSystem: ct.nlsys,
ct.StateSpace: ct.ss,
ct.TransferFunction: ct.tf,
}
#
# List of arguments described in class docstrings
#
# These are the minimal arguments needed to initialize the class. Optional
# arguments should be documented in the factory functions and do not need
# to be duplicated in the class documentation.
#
class_args = {
fs.FlatSystem: ['forward', 'reverse'],
ct.FrequencyResponseData: ['response', 'omega', 'dt'],
ct.NonlinearIOSystem: [
'updfcn', 'outfcn', 'inputs', 'outputs', 'states', 'params', 'dt'],
ct.StateSpace: ['A', 'B', 'C', 'D', 'dt'],
ct.TransferFunction: ['num', 'den', 'dt'],
}
#
# List of attributes described in class docstrings
#
# This is the list of attributes for the class that are not already listed
# as parameters used to initialize the class. These should all be defined
# in the class docstring.
#
# Attributes that are part of all I/O system classes should be listed in
# `std_class_attributes`. Attributes that are not commonly needed are
# defined as part of a parent class can just be documented there, and
# should be listed in `iosys_parent_attributes` (these will be searched
# using the MRO).
std_class_attributes = [
'ninputs', 'noutputs', 'input_labels', 'output_labels', 'name', 'shape']
# List of attributes defined for specific I/O systems
class_attributes = {
fs.FlatSystem: [],
ct.FrequencyResponseData: [],
ct.NonlinearIOSystem: ['nstates', 'state_labels'],
ct.StateSpace: ['nstates', 'state_labels'],
ct.TransferFunction: [],
}
# List of attributes defined in a parent class (no need to warn)
iosys_parent_attributes = [
'input_index', 'output_index', 'state_index', # rarely used
'states', 'nstates', 'state_labels', # not need in TF, FRD
'params', 'outfcn', 'updfcn' # NL I/O, SS overlap
]
#
# List of arguments described (only) in factory function docstrings
#
# These lists consist of the arguments that should be documented in the
# factory functions and should not be duplicated in the class
# documentation, even though in some cases they are actually processed in
# the class __init__ function.
#
std_factory_args = [
'inputs', 'outputs', 'name', 'input_prefix', 'output_prefix']
factory_args = {
fs.flatsys: ['states', 'state_prefix'],
ct.frd: ['sys'],
ct.nlsys: ['state_prefix'],
ct.ss: ['sys', 'states', 'state_prefix'],
ct.tf: ['sys'],
}
@pytest.mark.parametrize(
"cls, fcn, args",
[(cls, class_factory_function[cls], class_args[cls])
for cls in class_args.keys()])
def test_iosys_primary_classes(cls, fcn, args):
docstring = inspect.getdoc(cls)
# Make sure the typical arguments are there
for argname in args + std_class_attributes + class_attributes[cls]:
_check_parameter_docs(cls.__name__, argname, docstring)
# Make sure we reference the factory function
if re.search(
r"created.*(with|by|using).*the[\s]*"
f":func:`~control\\..*{fcn.__name__}`"
r"[\s]factory[\s]function", docstring, re.DOTALL) is None:
pytest.fail(
f"{cls.__name__} does not reference factory function "
f"{fcn.__name__}")
# Make sure we don't reference parameters from the factory function
for argname in factory_args[fcn]:
if re.search(f"[\\s]+{argname}(, .*)*[\\s]*:", docstring) is not None:
pytest.fail(
f"{cls.__name__} references factory function parameter "
f"'{argname}'")
@pytest.mark.parametrize("cls", class_args.keys())
def test_iosys_attribute_lists(cls, ignore_future_warning):
fcn = class_factory_function[cls]
# Create a system that we can scan for attributes
sys = ct.rss(2, 1, 1)
ignore_args = []
match fcn:
case ct.tf:
sys = ct.tf(sys)
ignore_args = ['state_labels']
case ct.frd:
sys = ct.frd(sys, [0.1, 1, 10])
ignore_args = ['state_labels']
case ct.nlsys:
sys = ct.nlsys(sys)
case fs.flatsys:
sys = fs.flatsys(sys)
sys = fs.flatsys(sys.forward, sys.reverse)
docstring = inspect.getdoc(cls)
for name, obj in inspect.getmembers(sys):
if name.startswith('_') or inspect.ismethod(obj) or name in ignore_args:
# Skip hidden variables; class methods are checked elsewhere
continue
# Try to find documentation in primary class
if _check_parameter_docs(
cls.__name__, name, docstring, fail_if_missing=False):
continue
# Couldn't find in main documentation; look in parent classes
for parent in cls.__mro__:
if parent == object:
pytest.fail(
f"{cls.__name__} attribute '{name}' not documented")
if _check_parameter_docs(
parent.__name__, name, inspect.getdoc(parent),
fail_if_missing=False):
if name not in iosys_parent_attributes + factory_args[fcn]:
warnings.warn(
f"{cls.__name__} attribute '{name}' only documented "
f"in parent class {parent.__name__}")
break
@pytest.mark.parametrize("cls", [ct.InputOutputSystem, ct.LTI])
def test_iosys_container_classes(cls):
# Create a system that we can scan for attributes
sys = cls(states=2, outputs=1, inputs=1)
docstring = inspect.getdoc(cls)
for name, obj in inspect.getmembers(sys):
if name.startswith('_') or inspect.ismethod(obj):
# Skip hidden variables; class methods are checked elsewhere
continue
# Look through all classes in hierarchy
if verbose:
print(f"{name=}")
for parent in cls.__mro__:
if parent == object:
pytest.fail(
f"{cls.__name__} attribute '{name}' not documented")
if verbose:
print(f" {parent=}")
if _check_parameter_docs(
parent.__name__, name, inspect.getdoc(parent),
fail_if_missing=False):
break
@pytest.mark.parametrize("cls", [ct.InterconnectedSystem, ct.LinearICSystem])
def test_iosys_intermediate_classes(cls):
docstring = inspect.getdoc(cls)
# Make sure there is not a parameters section
if re.search(r"\nParameters\n----", docstring) is not None:
pytest.fail(f"intermediate {cls} docstring contains Parameters section")
# Make sure we reference the factory function
fcn = class_factory_function[cls]
if re.search(f":func:`~control.{fcn.__name__}`", docstring) is None:
pytest.fail(
f"{cls.__name__} does not reference factory function "
f"{fcn.__name__}")
@pytest.mark.parametrize("fcn", factory_args.keys())
def test_iosys_factory_functions(fcn):
docstring = inspect.getdoc(fcn)
cls = list(class_factory_function.keys())[
list(class_factory_function.values()).index(fcn)]
# Make sure we reference parameters in class and factory function docstring
for argname in class_args[cls] + std_factory_args + factory_args[fcn]:
_check_parameter_docs(fcn.__name__, argname, docstring)
# Make sure we don't reference any class attributes
for argname in std_class_attributes + class_attributes[cls]:
if argname in std_factory_args:
continue
if re.search(f"[\\s]+{argname}(, .*)*[\\s]*:", docstring) is not None:
pytest.fail(
f"{fcn.__name__} references class attribute '{argname}'")
# Utility function to check for an argument in a docstring
def _check_parameter_docs(
funcname, argname, docstring, prefix="", fail_if_missing=True):
funcname = prefix + funcname
# Find the "Parameters" section of docstring, where we start searching
if not (match := re.search(
r"\nParameters\n----", docstring)):
if fail_if_missing:
pytest.fail(f"{funcname} docstring missing Parameters section")
else:
return False
else:
start = match.start()
# Find the "Returns" section of the docstring (to be skipped, if present)
match_returns = re.search(r"\nReturns\n----", docstring)
# Find the "Other Parameters" section of the docstring, if present
match_other = re.search(r"\nOther Parameters\n----", docstring)
# Remove the returns section from docstring, in case output arguments
# match input argument names (it happens...)
if match_other and match_returns:
docstring = docstring[start:match_returns.start()] + \
docstring[match_other.start():]
else:
docstring = docstring[start:]
# Look for the parameter name in the docstring
if match := re.search(
"\n" + r"((\w+|\.{3}), )*" + argname + r"(, (\w+|\.{3}))*:",
docstring):
# Found the string, but not in numpydoc form
if verbose:
print(f" {funcname}: {argname} docstring missing space")
warnings.warn(f"{funcname} '{argname}' docstring missing space")
elif not (match := re.search(
"\n" + r"((\w+|\.{3}), )*" + argname + r"(, (\w+|\.{3}))* :",
docstring)):
if verbose:
print(f" {funcname}: {argname} not documented")
if fail_if_missing:
pytest.fail(f"{funcname} '{argname}' not documented")
else:
return False
# Make sure there isn't another instance
second_match = re.search(
"\n" + r"((\w+|\.{3}), )*" + argname + r"(, (\w+|\.{3}))*[ ]*:",
docstring[match.end():])
if second_match:
pytest.fail(f"{funcname} '{argname}' documented twice")
return True