1313from importlib .abc import FileLoader
1414from importlib .machinery import EXTENSION_SUFFIXES
1515from importlib .util import decode_source , find_spec , module_from_spec , spec_from_loader
16+ from inspect import Parameter
1617from pathlib import Path
1718from types import ModuleType , SimpleNamespace
1819from typing import TYPE_CHECKING , NewType , TypeVar
3435 UNINITIALIZED_ATTR ,
3536)
3637from sphinx .ext .autodoc .mock import ismock , mock , undecorate
38+ from sphinx .ext .autodoc .type_comment import update_annotations_using_type_comments
3739from sphinx .locale import __
3840from sphinx .pycode import ModuleAnalyzer
3941from sphinx .util import inspect , logging
@@ -655,6 +657,46 @@ def _load_object_by_name(
655657 if inspect .isabstractmethod (obj ):
656658 obj_properties .add ('abstractmethod' )
657659
660+ # get property return type annotation
661+ obj_property_type_annotation = None
662+ if safe_getattr (obj , 'fget' , None ): # property
663+ func = obj .fget # type: ignore[union-attr]
664+ elif safe_getattr (obj , 'func' , None ): # cached_property
665+ func = obj .func # type: ignore[union-attr]
666+ else :
667+ func = None
668+ if func is not None :
669+ app = SimpleNamespace (config = config )
670+ # update the annotations of the property getter
671+ update_annotations_using_type_comments (app , func , False ) # type: ignore[arg-type]
672+
673+ try :
674+ signature = inspect .signature (
675+ func , type_aliases = config .autodoc_type_aliases
676+ )
677+ except TypeError as exc :
678+ full_name = '.' .join ((module_name , * parts ))
679+ logger .warning (
680+ __ ('Failed to get a function signature for %s: %s' ),
681+ full_name ,
682+ exc ,
683+ )
684+ pass
685+ except ValueError :
686+ pass
687+ else :
688+ if config .autodoc_typehints_format == 'short' :
689+ mode = 'smart'
690+ else :
691+ mode = 'fully-qualified-except-typing'
692+ if signature .return_annotation is not Parameter .empty :
693+ short_literals = config .python_display_short_literal_types
694+ obj_property_type_annotation = stringify_annotation (
695+ signature .return_annotation ,
696+ mode , # type: ignore[arg-type]
697+ short_literals = short_literals ,
698+ )
699+
658700 props = _FunctionDefProperties (
659701 obj_type = objtype ,
660702 module_name = module_name ,
@@ -665,6 +707,7 @@ def _load_object_by_name(
665707 _obj___module__ = get_attr (parent or obj , '__module__' , None ) or module_name ,
666708 _obj___name__ = getattr (parent or obj , '__name__' , None ),
667709 _obj___qualname__ = getattr (parent or obj , '__qualname__' , None ),
710+ _obj_property_type_annotation = obj_property_type_annotation ,
668711 )
669712 elif objtype == 'data' :
670713 # Update __annotations__ to support type_comment and so on
0 commit comments