Skip to content

Commit

Permalink
Add cached property support (#433)
Browse files Browse the repository at this point in the history
* TST: Add test case for cached_property.

* BUG: Fix detection of cached_property attrs.

Co-authored-by: Tirth Patel <tirthasheshpatel@gmail.com>

* Wrap cached_property import in try/except for Python 3.7.

Co-authored-by: Tirth Patel <tirthasheshpatel@gmail.com>
  • Loading branch information
rossbar and tirthasheshpatel committed Sep 28, 2022
1 parent 94b7a84 commit ddcf0fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
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():
Expand Down Expand Up @@ -706,7 +713,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)
Expand Down
21 changes: 21 additions & 0 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import namedtuple
from copy import deepcopy
import re
import sys
import textwrap
import warnings

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ddcf0fe

Please sign in to comment.