diff --git a/build_tools/azure/install.sh b/build_tools/azure/install.sh index 106043067d56a..d7492f41760a1 100755 --- a/build_tools/azure/install.sh +++ b/build_tools/azure/install.sh @@ -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 diff --git a/build_tools/circle/build_test_arm.sh b/build_tools/circle/build_test_arm.sh index a78d35adf4b60..c27353e548940 100755 --- a/build_tools/circle/build_test_arm.sh +++ b/build_tools/circle/build_test_arm.sh @@ -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 diff --git a/sklearn/_min_dependencies.py b/sklearn/_min_dependencies.py index f552de56617e1..968fb662c75e6 100644 --- a/sklearn/_min_dependencies.py +++ b/sklearn/_min_dependencies.py @@ -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"), diff --git a/sklearn/utils/_testing.py b/sklearn/utils/_testing.py index 1724063be2f43..7b57549e5886c 100644 --- a/sklearn/utils/_testing.py +++ b/sklearn/utils/_testing.py @@ -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"]: diff --git a/sklearn/utils/tests/test_testing.py b/sklearn/utils/tests/test_testing.py index a3a42aeb4c83f..ea4831fb02400 100644 --- a/sklearn/utils/tests/test_testing.py +++ b/sklearn/utils/tests/test_testing.py @@ -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 @@ -387,7 +387,7 @@ def f_bad_sections(self, X, y): """Function f Parameter - ---------- + --------- a : int Parameter a b : float @@ -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) @@ -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", "sklearn.utils.tests.test_testing.f_check_param_definition There " "was no space between the param name and colon ('d:int')", ] @@ -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",