Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cached property support #433

Merged
merged 3 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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