From 3dfc1820a8ba8cbaf0a5d25a77de21073f75f8c4 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Mon, 4 Nov 2019 00:51:45 +0100 Subject: [PATCH 1/3] MAINT Improve method dectection in test_docstrings.py --- maint_tools/test_docstrings.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/maint_tools/test_docstrings.py b/maint_tools/test_docstrings.py index 1a8c8b3297110..a31a3fba0ef47 100644 --- a/maint_tools/test_docstrings.py +++ b/maint_tools/test_docstrings.py @@ -38,7 +38,13 @@ def get_all_methods(): if name.startswith("_"): # skip private classes continue - methods = [el for el in dir(estimator) if not el.startswith("_")] + methods = [] + for name in dir(estimator): + if name.startswith("_"): + continue + method_obj = getattr(estimator, name) + if hasattr(method_obj, '__call__') or name == "predict_proba": + methods.append(name) methods.append(None) for method in sorted(methods, key=lambda x: str(x)): @@ -102,7 +108,15 @@ def repr_errors(res, estimator=None, method: Optional[str] = None) -> str: raise NotImplementedError if estimator is not None: - obj_signature = signature(getattr(estimator, method)) + obj = getattr(estimator, method) + try: + obj_signature = signature(obj) + except TypeError: + # In particular we can't parse the signature + # for properties that are still callable such as + # predict_proba + obj_signature = "Parsing of the method signature failed." + obj_name = estimator.__name__ + "." + method else: obj_signature = "" @@ -110,7 +124,7 @@ def repr_errors(res, estimator=None, method: Optional[str] = None) -> str: msg = "\n\n" + "\n\n".join( [ - res["file"], + str(res["file"]), obj_name + str(obj_signature), res["docstring"], "# Errors", From 23c87c9e4294e9bde7da36403de97bee36e79819 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Mon, 4 Nov 2019 01:02:54 +0100 Subject: [PATCH 2/3] Fix for properties --- maint_tools/test_docstrings.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/maint_tools/test_docstrings.py b/maint_tools/test_docstrings.py index a31a3fba0ef47..f92db30c96ade 100644 --- a/maint_tools/test_docstrings.py +++ b/maint_tools/test_docstrings.py @@ -34,21 +34,22 @@ def get_all_methods(): estimators = all_estimators() - for name, estimator in estimators: + for name, Estimator in estimators: if name.startswith("_"): # skip private classes continue methods = [] - for name in dir(estimator): + for name in dir(Estimator): if name.startswith("_"): continue - method_obj = getattr(estimator, name) - if hasattr(method_obj, '__call__') or name == "predict_proba": + method_obj = getattr(Estimator, name) + if (hasattr(method_obj, '__call__') + or isinstance(method_obj, property)): methods.append(name) methods.append(None) for method in sorted(methods, key=lambda x: str(x)): - yield estimator, method + yield Estimator, method def filter_errors(errors, method): @@ -115,7 +116,10 @@ def repr_errors(res, estimator=None, method: Optional[str] = None) -> str: # In particular we can't parse the signature # for properties that are still callable such as # predict_proba - obj_signature = "Parsing of the method signature failed." + obj_signature = ( + "\nParsing of the method signature failed, " + "possibly because this is a property." + ) obj_name = estimator.__name__ + "." + method else: @@ -137,10 +141,10 @@ def repr_errors(res, estimator=None, method: Optional[str] = None) -> str: return msg -@pytest.mark.parametrize("estimator, method", get_all_methods()) -def test_docstring(estimator, method, request): - base_import_path = estimator.__module__ - import_path = [base_import_path, estimator.__name__] +@pytest.mark.parametrize("Estimator, method", get_all_methods()) +def test_docstring(Estimator, method, request): + base_import_path = Estimator.__module__ + import_path = [base_import_path, Estimator.__name__] if method is not None: import_path.append(method) @@ -158,7 +162,7 @@ def test_docstring(estimator, method, request): res["errors"] = list(filter_errors(res["errors"], method)) if res["errors"]: - msg = repr_errors(res, estimator, method) + msg = repr_errors(res, Estimator, method) raise ValueError(msg) From bde97501cd8f87d426460ae54c16f67c70f49526 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Mon, 4 Nov 2019 10:09:50 +0100 Subject: [PATCH 3/3] Improve comment --- maint_tools/test_docstrings.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maint_tools/test_docstrings.py b/maint_tools/test_docstrings.py index f92db30c96ade..2861303c796b4 100644 --- a/maint_tools/test_docstrings.py +++ b/maint_tools/test_docstrings.py @@ -113,9 +113,7 @@ def repr_errors(res, estimator=None, method: Optional[str] = None) -> str: try: obj_signature = signature(obj) except TypeError: - # In particular we can't parse the signature - # for properties that are still callable such as - # predict_proba + # In particular we can't parse the signature of properties obj_signature = ( "\nParsing of the method signature failed, " "possibly because this is a property."