From 9c8fbd33bce3cc8307c0a0c62565f2bc40ba8a10 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Tue, 27 Sep 2022 16:19:47 -0700 Subject: [PATCH 1/3] TST: Add test case for cached_property. --- numpydoc/tests/test_docscrape.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 049d2a28..227f8724 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -1,6 +1,7 @@ from collections import namedtuple from copy import deepcopy import re +import sys import textwrap import warnings @@ -1624,6 +1625,26 @@ def __call__(self): nds._error_location(msg=msg) +@pytest.mark.skipif( + sys.version_info < (3, 8), reason="cached_property was added in 3.8" +) +def test_class_docstring_cached_property(): + """Ensure that properties marked with the `cached_property` decorator + are listed in the Methods section. See gh-432.""" + from functools import cached_property + + class Foo: + _x = [1, 2, 3] + + @cached_property + def val(self): + return self._x + + class_docstring = get_doc_object(Foo) + assert len(class_docstring["Attributes"]) == 1 + assert class_docstring["Attributes"][0].name == "val" + + if __name__ == "__main__": import pytest From 83dcba0934c60029091b00842ada32090fce6dce Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Tue, 27 Sep 2022 16:27:45 -0700 Subject: [PATCH 2/3] BUG: Fix detection of cached_property attrs. Co-authored-by: Tirth Patel --- numpydoc/docscrape.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index 9496f9de..72de466e 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -8,6 +8,7 @@ from warnings import warn from collections import namedtuple from collections.abc import Callable, Mapping +from functools import cached_property import copy import sys @@ -706,7 +707,7 @@ def properties(self): not name.startswith("_") and ( func is None - or isinstance(func, property) + or isinstance(func, (property, cached_property)) or inspect.isdatadescriptor(func) ) and self._is_show_member(name) From f36cb83c5de6e947f316544f528a842f86b8b4c5 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Tue, 27 Sep 2022 16:36:59 -0700 Subject: [PATCH 3/3] Wrap cached_property import in try/except for Python 3.7. --- numpydoc/docscrape.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index 72de466e..e5c07f59 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -8,11 +8,17 @@ from warnings import warn from collections import namedtuple from collections.abc import Callable, Mapping -from functools import cached_property import copy import sys +# TODO: Remove try-except when support for Python 3.7 is dropped +try: + from functools import cached_property +except ImportError: # cached_property added in Python 3.8 + cached_property = property + + def strip_blank_lines(l): "Remove leading and trailing blank lines from a list of lines" while l and not l[0].strip():