From 65c089bd4094acb7ccae887c9b9d706c8a390d5c Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 22 Jul 2021 23:25:02 +0900 Subject: [PATCH] Fix #9487: autodoc: typehint for cached_property is not shown --- CHANGES | 2 ++ sphinx/ext/autodoc/__init__.py | 11 +++++++++-- tests/test_ext_autodoc.py | 1 + tests/test_ext_autodoc_autoproperty.py | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 8289c5eb9b4..34a4c09d90a 100644 --- a/CHANGES +++ b/CHANGES @@ -20,6 +20,8 @@ Features added Bugs fixed ---------- +* #9487: autodoc: typehint for cached_property is not shown + Testing -------- diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index fd5ca62a2ff..30076a9c4dd 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -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) diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index 3bb58cde3bf..299c1c68170 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -1084,6 +1084,7 @@ def test_autodoc_cached_property(app): '', ' .. py:property:: Foo.prop', ' :module: target.cached_property', + ' :type: int', '', ] diff --git a/tests/test_ext_autodoc_autoproperty.py b/tests/test_ext_autodoc_autoproperty.py index 2b4e5c12a97..47528a99d8c 100644 --- a/tests/test_ext_autodoc_autoproperty.py +++ b/tests/test_ext_autodoc_autoproperty.py @@ -9,6 +9,8 @@ :license: BSD, see LICENSE for details. """ +import sys + import pytest from .test_ext_autodoc import do_autodoc @@ -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', + '', + ]