Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG] CI Reinstate docstring testing #10473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ matrix:
# It also runs tests requiring Pandas and PyAMG
- env: DISTRIB="conda" PYTHON_VERSION="3.6.2" INSTALL_MKL="true"
NUMPY_VERSION="1.13.1" SCIPY_VERSION="0.19.1" PANDAS_VERSION="0.20.3"
CYTHON_VERSION="0.26.1" PYAMG_VERSION="3.3.2" PILLOW_VERSION="4.3.0"
COVERAGE=true CHECK_PYTEST_SOFT_DEPENDENCY="true"
CYTHON_VERSION="0.26.1" PYAMG_VERSIONi="3.3.2" PILLOW_VERSION="4.3.0"
COVERAGE=true
CHECK_PYTEST_SOFT_DEPENDENCY="true" TEST_DOCSTRINGS="true"
if: type != cron
# flake8 linting on diff wrt common ancestor with upstream/master
- env: RUN_FLAKE8="true" SKIP_TESTS="true"
Expand Down
23 changes: 8 additions & 15 deletions sklearn/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,23 +862,16 @@ def check_docstring_parameters(func, doc=None, ignore=None, class_name=None):

param_names = []
for name, type_definition, param_doc in doc['Parameters']:
if (type_definition.strip() == "" or
type_definition.strip().startswith(':')):

param_name = name.lstrip()

# If there was no space between name and the colon
# "verbose:" -> len(["verbose", ""][0]) -> 7
# If "verbose:"[7] == ":", then there was no space
if (':' not in param_name or
param_name[len(param_name.split(':')[0].strip())] == ':'):
if not type_definition.strip():
if ':' in name and name[:name.index(':')][-1:].strip():
incorrect += [func_name +
' There was no space between the param name and '
'colon ("%s")' % name]
else:
incorrect += [func_name + ' Incorrect type definition for '
'param: "%s" (type definition was "%s")'
% (name.split(':')[0], type_definition)]
'colon (%r)' % name]
elif name.rstrip().endswith(':'):
incorrect += [func_name +
' Parameter %r has an empty type spec. '
'Remove the colon' % (name.lstrip())]

if '*' not in name:
param_names.append(name.split(':')[0].strip('` '))

Expand Down
57 changes: 22 additions & 35 deletions sklearn/utils/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def f_missing(a, b):
return c


def f_check_param_definition(a, b, c, d):
def f_check_param_definition(a, b, c, d, e):
"""Function f

Parameters
Expand All @@ -338,6 +338,8 @@ def f_check_param_definition(a, b, c, d):
Parameter c
d:int
Parameter d
e
No typespec is allowed without colon
"""
return a + b + c + d

Expand Down Expand Up @@ -402,8 +404,8 @@ def predict(self, X):
"""
return self.delegate.predict(X)

@deprecated("Testing a deprecated delegated method")
@if_delegate_has_method(delegate=('delegate'))
@deprecated("Testing a deprecated delegated method")
def score(self, X):
"""This is available only if delegate has score.

Expand All @@ -424,20 +426,7 @@ def predict_proba(self, X):
"""
return X

@deprecated('Testing deprecated function with incorrect params')
@if_delegate_has_method(delegate=('delegate'))
def predict_log_proba(self, X):
"""This is available only if delegate has predict_proba.

Parameters
---------
y : ndarray
Parameter X
"""
return X

@deprecated('Testing deprecated function with wrong params')
@if_delegate_has_method(delegate=('delegate'))
def fit(self, X, y):
"""Incorrect docstring but should not be tested"""

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

incorrect = check_docstring_parameters(f_ok)
assert_equal(incorrect, [])
assert incorrect == []
incorrect = check_docstring_parameters(f_ok, ignore=['b'])
assert_equal(incorrect, [])
assert incorrect == []
incorrect = check_docstring_parameters(f_missing, ignore=['b'])
assert_equal(incorrect, [])
assert incorrect == []
assert_raise_message(RuntimeError, 'Unknown section Results',
check_docstring_parameters, f_bad_sections)
assert_raise_message(RuntimeError, 'Unknown section Parameter',
check_docstring_parameters, Klass.f_bad_sections)

incorrect = check_docstring_parameters(f_check_param_definition)
assert (
incorrect == [
"sklearn.utils.tests.test_testing.f_check_param_definition There "
"was no space between the param name and colon ('a: int')",
"sklearn.utils.tests.test_testing.f_check_param_definition There "
"was no space between the param name and colon ('b:')",
"sklearn.utils.tests.test_testing.f_check_param_definition "
"Parameter 'c :' has an empty type spec. Remove the colon",
"sklearn.utils.tests.test_testing.f_check_param_definition There "
"was no space between the param name and colon ('d:int')",
])

messages = ["a != b", "arg mismatch: ['b']", "arg mismatch: ['X', 'y']",
"predict y != X",
"predict_proba arg mismatch: ['X']",
"predict_log_proba arg mismatch: ['X']",
"score arg mismatch: ['X']",
".fit arg mismatch: ['X', 'y']"]

Expand All @@ -473,21 +474,7 @@ def test_check_docstring_parameters():
for mess, f in zip(messages,
[f_bad_order, f_missing, Klass.f_missing,
mock_meta.predict, mock_meta.predict_proba,
mock_meta.predict_log_proba,
mock_meta.score, mock_meta.fit]):
incorrect = check_docstring_parameters(f)
assert_true(len(incorrect) >= 1)
assert_true(mess in incorrect[0],
'"%s" not in "%s"' % (mess, incorrect[0]))

incorrect = check_docstring_parameters(f_check_param_definition)
assert_equal(
incorrect,
['sklearn.utils.tests.test_testing.f_check_param_definition There was '
'no space between the param name and colon ("a: int")',
'sklearn.utils.tests.test_testing.f_check_param_definition There was '
'no space between the param name and colon ("b:")',
'sklearn.utils.tests.test_testing.f_check_param_definition Incorrect '
'type definition for param: "c " (type definition was "")',
'sklearn.utils.tests.test_testing.f_check_param_definition There was '
'no space between the param name and colon ("d:int")'])
assert len(incorrect) >= 1
assert mess in incorrect[0], '"%s" not in "%s"' % (mess, incorrect[0])