Skip to content

Commit ddcf0fe

Browse files
Add cached property support (#433)
* TST: Add test case for cached_property. * BUG: Fix detection of cached_property attrs. Co-authored-by: Tirth Patel <[email protected]> * Wrap cached_property import in try/except for Python 3.7. Co-authored-by: Tirth Patel <[email protected]>
1 parent 94b7a84 commit ddcf0fe

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

numpydoc/docscrape.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
import sys
1313

1414

15+
# TODO: Remove try-except when support for Python 3.7 is dropped
16+
try:
17+
from functools import cached_property
18+
except ImportError: # cached_property added in Python 3.8
19+
cached_property = property
20+
21+
1522
def strip_blank_lines(l):
1623
"Remove leading and trailing blank lines from a list of lines"
1724
while l and not l[0].strip():
@@ -706,7 +713,7 @@ def properties(self):
706713
not name.startswith("_")
707714
and (
708715
func is None
709-
or isinstance(func, property)
716+
or isinstance(func, (property, cached_property))
710717
or inspect.isdatadescriptor(func)
711718
)
712719
and self._is_show_member(name)

numpydoc/tests/test_docscrape.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import namedtuple
22
from copy import deepcopy
33
import re
4+
import sys
45
import textwrap
56
import warnings
67

@@ -1624,6 +1625,26 @@ def __call__(self):
16241625
nds._error_location(msg=msg)
16251626

16261627

1628+
@pytest.mark.skipif(
1629+
sys.version_info < (3, 8), reason="cached_property was added in 3.8"
1630+
)
1631+
def test_class_docstring_cached_property():
1632+
"""Ensure that properties marked with the `cached_property` decorator
1633+
are listed in the Methods section. See gh-432."""
1634+
from functools import cached_property
1635+
1636+
class Foo:
1637+
_x = [1, 2, 3]
1638+
1639+
@cached_property
1640+
def val(self):
1641+
return self._x
1642+
1643+
class_docstring = get_doc_object(Foo)
1644+
assert len(class_docstring["Attributes"]) == 1
1645+
assert class_docstring["Attributes"][0].name == "val"
1646+
1647+
16271648
if __name__ == "__main__":
16281649
import pytest
16291650

0 commit comments

Comments
 (0)