Skip to content

Commit 65c089b

Browse files
committed
Fix #9487: autodoc: typehint for cached_property is not shown
1 parent f9941b9 commit 65c089b

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Features added
2020
Bugs fixed
2121
----------
2222

23+
* #9487: autodoc: typehint for cached_property is not shown
24+
2325
Testing
2426
--------
2527

sphinx/ext/autodoc/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,9 +2703,16 @@ def add_directive_header(self, sig: str) -> None:
27032703
if self.isclassmethod:
27042704
self.add_line(' :classmethod:', sourcename)
27052705

2706-
if safe_getattr(self.object, 'fget', None) and self.config.autodoc_typehints != 'none':
2706+
if safe_getattr(self.object, 'fget', None): # property
2707+
func = self.object.fget
2708+
elif safe_getattr(self.object, 'func', None): # cached_property
2709+
func = self.object.func
2710+
else:
2711+
func = None
2712+
2713+
if func and self.config.autodoc_typehints != 'none':
27072714
try:
2708-
signature = inspect.signature(self.object.fget,
2715+
signature = inspect.signature(func,
27092716
type_aliases=self.config.autodoc_type_aliases)
27102717
if signature.return_annotation is not Parameter.empty:
27112718
objrepr = stringify_typehint(signature.return_annotation)

tests/test_ext_autodoc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ def test_autodoc_cached_property(app):
10841084
'',
10851085
' .. py:property:: Foo.prop',
10861086
' :module: target.cached_property',
1087+
' :type: int',
10871088
'',
10881089
]
10891090

tests/test_ext_autodoc_autoproperty.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
:license: BSD, see LICENSE for details.
1010
"""
1111

12+
import sys
13+
1214
import pytest
1315

1416
from .test_ext_autodoc import do_autodoc
@@ -41,3 +43,16 @@ def test_class_properties(app):
4143
' docstring',
4244
'',
4345
]
46+
47+
48+
@pytest.mark.skipif(sys.version_info < (3, 8), reason='python 3.8+ is required.')
49+
@pytest.mark.sphinx('html', testroot='ext-autodoc')
50+
def test_cached_properties(app):
51+
actual = do_autodoc(app, 'property', 'target.cached_property.Foo.prop')
52+
assert list(actual) == [
53+
'',
54+
'.. py:property:: Foo.prop',
55+
' :module: target.cached_property',
56+
' :type: int',
57+
'',
58+
]

0 commit comments

Comments
 (0)