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

Skip to content

MAINT Adjust tests for numpydoc 1.2 #22287

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 12 commits into from
Feb 11, 2022
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
4 changes: 1 addition & 3 deletions build_tools/azure/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ fi
if [[ "$TEST_DOCSTRINGS" == "true" ]]; then
# numpydoc requires sphinx
python -m pip install sphinx
# TODO: update the docstring checks to be compatible with new
# numpydoc versions
python -m pip install "numpydoc<1.2"
python -m pip install numpydoc
fi

python --version
Expand Down
2 changes: 1 addition & 1 deletion build_tools/circle/build_test_arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fi
if [[ "$TEST_DOCSTRINGS" == "true" ]]; then
# numpydoc requires sphinx
mamba install --verbose -y sphinx
mamba install --verbose -y "numpydoc<1.2"
mamba install --verbose -y numpydoc
fi

python --version
Expand Down
2 changes: 1 addition & 1 deletion sklearn/_min_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"pyamg": ("4.0.0", "tests"),
"sphinx": ("4.0.1", "docs"),
"sphinx-gallery": ("0.7.0", "docs"),
"numpydoc": ("1.0.0", "docs"),
"numpydoc": ("1.2.0", "docs, tests"),
"Pillow": ("7.1.2", "docs"),
"sphinx-prompt": ("1.3.0", "docs"),
"sphinxext-opengraph": ("0.4.2", "docs"),
Expand Down
17 changes: 14 additions & 3 deletions sklearn/utils/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,25 @@ def check_docstring_parameters(func, doc=None, ignore=None):

# Analyze function's docstring
if doc is None:
with warnings.catch_warnings(record=True) as w:
records = []
with warnings.catch_warnings(record=True):
warnings.simplefilter("error", UserWarning)
try:
doc = docscrape.FunctionDoc(func)
except UserWarning as exp:
if "potentially wrong underline length" in str(exp):
# Catch warning raised as of numpydoc 1.2 when
# the underline length for a section of a docstring
# is not consistent.
message = str(exp).split("\n")[:3]
incorrect += [f"In function: {func_name}"] + message
return incorrect
records.append(str(exp))
except Exception as exp:
incorrect += [func_name + " parsing error: " + str(exp)]
return incorrect
if len(w):
raise RuntimeError("Error for %s:\n%s" % (func_name, w[0]))
if len(records):
raise RuntimeError("Error for %s:\n%s" % (func_name, records[0]))

param_docs = []
for name, type_definition, param_doc in doc["Parameters"]:
Expand Down
26 changes: 11 additions & 15 deletions sklearn/utils/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def f_check_param_definition(a, b, c, d, e):
b:
Parameter b
c :
Parameter c
This is parsed correctly in numpydoc 1.2
d:int
Parameter d
e
Expand All @@ -387,7 +387,7 @@ def f_bad_sections(self, X, y):
"""Function f

Parameter
----------
---------
a : int
Parameter a
b : float
Expand Down Expand Up @@ -525,7 +525,9 @@ def fit(self, X, y):
)
def test_check_docstring_parameters(mock_meta):
pytest.importorskip(
"numpydoc", reason="numpydoc is required to test the docstrings"
"numpydoc",
reason="numpydoc is required to test the docstrings",
minversion="1.2.0",
)

incorrect = check_docstring_parameters(f_ok)
Expand All @@ -546,8 +548,6 @@ def test_check_docstring_parameters(mock_meta):
"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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In numpydoc 1.2, the c : is now parsed into c + empty type spec.

"sklearn.utils.tests.test_testing.f_check_param_definition There "
"was no space between the param name and colon ('d:int')",
]
Expand Down Expand Up @@ -602,20 +602,16 @@ def test_check_docstring_parameters(mock_meta):
"In function: "
+ f"sklearn.utils.tests.test_testing.{mock_meta_name}."
+ "predict_proba",
"Parameters in function docstring have less items w.r.t. function"
" signature, first missing item: X",
"Full diff:",
"- ['X']",
"+ []",
"potentially wrong underline length... ",
"Parameters ",
"--------- in ",
],
[
"In function: "
+ f"sklearn.utils.tests.test_testing.{mock_meta_name}.score",
"Parameters in function docstring have less items w.r.t. function"
" signature, first missing item: X",
"Full diff:",
"- ['X']",
"+ []",
"potentially wrong underline length... ",
"Parameters ",
"--------- in ",
],
[
"In function: " + f"sklearn.utils.tests.test_testing.{mock_meta_name}.fit",
Expand Down