Skip to content

Commit

Permalink
Fix #9487: autodoc: typehint for cached_property is not shown
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Jul 22, 2021
1 parent f9941b9 commit 65c089b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -20,6 +20,8 @@ Features added
Bugs fixed
----------

* #9487: autodoc: typehint for cached_property is not shown

Testing
--------

Expand Down
11 changes: 9 additions & 2 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -2703,9 +2703,16 @@ def add_directive_header(self, sig: str) -> None:
if self.isclassmethod:
self.add_line(' :classmethod:', sourcename)

if safe_getattr(self.object, 'fget', None) and self.config.autodoc_typehints != 'none':
if safe_getattr(self.object, 'fget', None): # property
func = self.object.fget
elif safe_getattr(self.object, 'func', None): # cached_property
func = self.object.func
else:
func = None

if func and self.config.autodoc_typehints != 'none':
try:
signature = inspect.signature(self.object.fget,
signature = inspect.signature(func,
type_aliases=self.config.autodoc_type_aliases)
if signature.return_annotation is not Parameter.empty:
objrepr = stringify_typehint(signature.return_annotation)
Expand Down
1 change: 1 addition & 0 deletions tests/test_ext_autodoc.py
Expand Up @@ -1084,6 +1084,7 @@ def test_autodoc_cached_property(app):
'',
' .. py:property:: Foo.prop',
' :module: target.cached_property',
' :type: int',
'',
]

Expand Down
15 changes: 15 additions & 0 deletions tests/test_ext_autodoc_autoproperty.py
Expand Up @@ -9,6 +9,8 @@
:license: BSD, see LICENSE for details.
"""

import sys

import pytest

from .test_ext_autodoc import do_autodoc
Expand Down Expand Up @@ -41,3 +43,16 @@ def test_class_properties(app):
' docstring',
'',
]


@pytest.mark.skipif(sys.version_info < (3, 8), reason='python 3.8+ is required.')
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_cached_properties(app):
actual = do_autodoc(app, 'property', 'target.cached_property.Foo.prop')
assert list(actual) == [
'',
'.. py:property:: Foo.prop',
' :module: target.cached_property',
' :type: int',
'',
]

0 comments on commit 65c089b

Please sign in to comment.