Skip to content

Commit 116c96c

Browse files
jnothmanglemaitre
authored andcommitted
[MRG] CI Reinstate docstring testing (#10473)
1 parent 205275b commit 116c96c

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ matrix:
4242
# It also runs tests requiring Pandas and PyAMG
4343
- env: DISTRIB="conda" PYTHON_VERSION="3.6.2" INSTALL_MKL="true"
4444
NUMPY_VERSION="1.13.1" SCIPY_VERSION="0.19.1" PANDAS_VERSION="0.20.3"
45-
CYTHON_VERSION="0.26.1" PYAMG_VERSION="3.3.2" PILLOW_VERSION="4.3.0"
46-
COVERAGE=true CHECK_PYTEST_SOFT_DEPENDENCY="true"
45+
CYTHON_VERSION="0.26.1" PYAMG_VERSIONi="3.3.2" PILLOW_VERSION="4.3.0"
46+
COVERAGE=true
47+
CHECK_PYTEST_SOFT_DEPENDENCY="true" TEST_DOCSTRINGS="true"
4748
if: type != cron
4849
# flake8 linting on diff wrt common ancestor with upstream/master
4950
- env: RUN_FLAKE8="true" SKIP_TESTS="true"

sklearn/utils/testing.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -862,23 +862,16 @@ def check_docstring_parameters(func, doc=None, ignore=None, class_name=None):
862862

863863
param_names = []
864864
for name, type_definition, param_doc in doc['Parameters']:
865-
if (type_definition.strip() == "" or
866-
type_definition.strip().startswith(':')):
867-
868-
param_name = name.lstrip()
869-
870-
# If there was no space between name and the colon
871-
# "verbose:" -> len(["verbose", ""][0]) -> 7
872-
# If "verbose:"[7] == ":", then there was no space
873-
if (':' not in param_name or
874-
param_name[len(param_name.split(':')[0].strip())] == ':'):
865+
if not type_definition.strip():
866+
if ':' in name and name[:name.index(':')][-1:].strip():
875867
incorrect += [func_name +
876868
' There was no space between the param name and '
877-
'colon ("%s")' % name]
878-
else:
879-
incorrect += [func_name + ' Incorrect type definition for '
880-
'param: "%s" (type definition was "%s")'
881-
% (name.split(':')[0], type_definition)]
869+
'colon (%r)' % name]
870+
elif name.rstrip().endswith(':'):
871+
incorrect += [func_name +
872+
' Parameter %r has an empty type spec. '
873+
'Remove the colon' % (name.lstrip())]
874+
882875
if '*' not in name:
883876
param_names.append(name.split(':')[0].strip('` '))
884877

sklearn/utils/tests/test_testing.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def f_missing(a, b):
325325
return c
326326

327327

328-
def f_check_param_definition(a, b, c, d):
328+
def f_check_param_definition(a, b, c, d, e):
329329
"""Function f
330330
331331
Parameters
@@ -338,6 +338,8 @@ def f_check_param_definition(a, b, c, d):
338338
Parameter c
339339
d:int
340340
Parameter d
341+
e
342+
No typespec is allowed without colon
341343
"""
342344
return a + b + c + d
343345

@@ -402,8 +404,8 @@ def predict(self, X):
402404
"""
403405
return self.delegate.predict(X)
404406

405-
@deprecated("Testing a deprecated delegated method")
406407
@if_delegate_has_method(delegate=('delegate'))
408+
@deprecated("Testing a deprecated delegated method")
407409
def score(self, X):
408410
"""This is available only if delegate has score.
409411
@@ -424,20 +426,7 @@ def predict_proba(self, X):
424426
"""
425427
return X
426428

427-
@deprecated('Testing deprecated function with incorrect params')
428-
@if_delegate_has_method(delegate=('delegate'))
429-
def predict_log_proba(self, X):
430-
"""This is available only if delegate has predict_proba.
431-
432-
Parameters
433-
---------
434-
y : ndarray
435-
Parameter X
436-
"""
437-
return X
438-
439429
@deprecated('Testing deprecated function with wrong params')
440-
@if_delegate_has_method(delegate=('delegate'))
441430
def fit(self, X, y):
442431
"""Incorrect docstring but should not be tested"""
443432

@@ -451,20 +440,32 @@ def test_check_docstring_parameters():
451440
"numpydoc is required to test the docstrings")
452441

453442
incorrect = check_docstring_parameters(f_ok)
454-
assert_equal(incorrect, [])
443+
assert incorrect == []
455444
incorrect = check_docstring_parameters(f_ok, ignore=['b'])
456-
assert_equal(incorrect, [])
445+
assert incorrect == []
457446
incorrect = check_docstring_parameters(f_missing, ignore=['b'])
458-
assert_equal(incorrect, [])
447+
assert incorrect == []
459448
assert_raise_message(RuntimeError, 'Unknown section Results',
460449
check_docstring_parameters, f_bad_sections)
461450
assert_raise_message(RuntimeError, 'Unknown section Parameter',
462451
check_docstring_parameters, Klass.f_bad_sections)
463452

453+
incorrect = check_docstring_parameters(f_check_param_definition)
454+
assert (
455+
incorrect == [
456+
"sklearn.utils.tests.test_testing.f_check_param_definition There "
457+
"was no space between the param name and colon ('a: int')",
458+
"sklearn.utils.tests.test_testing.f_check_param_definition There "
459+
"was no space between the param name and colon ('b:')",
460+
"sklearn.utils.tests.test_testing.f_check_param_definition "
461+
"Parameter 'c :' has an empty type spec. Remove the colon",
462+
"sklearn.utils.tests.test_testing.f_check_param_definition There "
463+
"was no space between the param name and colon ('d:int')",
464+
])
465+
464466
messages = ["a != b", "arg mismatch: ['b']", "arg mismatch: ['X', 'y']",
465467
"predict y != X",
466468
"predict_proba arg mismatch: ['X']",
467-
"predict_log_proba arg mismatch: ['X']",
468469
"score arg mismatch: ['X']",
469470
".fit arg mismatch: ['X', 'y']"]
470471

@@ -473,21 +474,7 @@ def test_check_docstring_parameters():
473474
for mess, f in zip(messages,
474475
[f_bad_order, f_missing, Klass.f_missing,
475476
mock_meta.predict, mock_meta.predict_proba,
476-
mock_meta.predict_log_proba,
477477
mock_meta.score, mock_meta.fit]):
478478
incorrect = check_docstring_parameters(f)
479-
assert_true(len(incorrect) >= 1)
480-
assert_true(mess in incorrect[0],
481-
'"%s" not in "%s"' % (mess, incorrect[0]))
482-
483-
incorrect = check_docstring_parameters(f_check_param_definition)
484-
assert_equal(
485-
incorrect,
486-
['sklearn.utils.tests.test_testing.f_check_param_definition There was '
487-
'no space between the param name and colon ("a: int")',
488-
'sklearn.utils.tests.test_testing.f_check_param_definition There was '
489-
'no space between the param name and colon ("b:")',
490-
'sklearn.utils.tests.test_testing.f_check_param_definition Incorrect '
491-
'type definition for param: "c " (type definition was "")',
492-
'sklearn.utils.tests.test_testing.f_check_param_definition There was '
493-
'no space between the param name and colon ("d:int")'])
479+
assert len(incorrect) >= 1
480+
assert mess in incorrect[0], '"%s" not in "%s"' % (mess, incorrect[0])

0 commit comments

Comments
 (0)