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

Fix #9487: autodoc: typehint for cached_property is not shown #9488

Merged
merged 2 commits into from Jul 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
'',
]