Skip to content

Commit 000210e

Browse files
authored
Merge pull request #8111 from tk0miya/8103_cached_property
Fix #8103: autodoc: cached_property is not considered as a property
2 parents 1d0b424 + dfbe687 commit 000210e

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Bugs fixed
2222
* #8085: i18n: Add support for having single text domain
2323
* #8143: autodoc: AttributeError is raised when False value is passed to
2424
autodoc_default_options
25+
* #8103: autodoc: functools.cached_property is not considered as a property
2526
* #8192: napoleon: description is disappeared when it contains inline literals
2627
* #8093: The highlight warning has wrong location in some builders (LaTeX,
2728
singlehtml and so on)

sphinx/util/inspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ def iscoroutinefunction(obj: Any) -> bool:
304304

305305
def isproperty(obj: Any) -> bool:
306306
"""Check if the object is property."""
307+
if sys.version_info > (3, 8):
308+
from functools import cached_property # cached_property is available since py3.8
309+
if isinstance(obj, cached_property):
310+
return True
311+
307312
return isinstance(obj, property)
308313

309314

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from functools import cached_property
2+
3+
4+
class Foo:
5+
@cached_property
6+
def prop(self) -> int:
7+
return 1

tests/test_ext_autodoc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,26 @@ def test_autodoc_descriptor(app):
881881
]
882882

883883

884+
@pytest.mark.skipif(sys.version_info < (3, 8),
885+
reason='cached_property is available since python3.8.')
886+
@pytest.mark.sphinx('html', testroot='ext-autodoc')
887+
def test_autodoc_cached_property(app):
888+
options = {"members": None,
889+
"undoc-members": True}
890+
actual = do_autodoc(app, 'class', 'target.cached_property.Foo', options)
891+
assert list(actual) == [
892+
'',
893+
'.. py:class:: Foo()',
894+
' :module: target.cached_property',
895+
'',
896+
'',
897+
' .. py:method:: Foo.prop',
898+
' :module: target.cached_property',
899+
' :property:',
900+
'',
901+
]
902+
903+
884904
@pytest.mark.sphinx('html', testroot='ext-autodoc')
885905
def test_autodoc_member_order(app):
886906
# case member-order='bysource'

0 commit comments

Comments
 (0)