diff --git a/sphinx/ext/autodoc/_docstrings.py b/sphinx/ext/autodoc/_docstrings.py index a1b1c84fd64..a1b924dd43a 100644 --- a/sphinx/ext/autodoc/_docstrings.py +++ b/sphinx/ext/autodoc/_docstrings.py @@ -88,7 +88,7 @@ def _prepare_docstrings( ) -> list[list[str]] | None: """Add content from docstrings, attribute documentation and user.""" # add content from attribute documentation - if props.obj_type not in {'data', 'attribute'} and props.parts: + if props.obj_type not in {'data', 'attribute', 'type'} and props.parts: key = ('.'.join(props.parent_names), props.name) try: # make a copy of docstring for attributes to avoid cache @@ -298,6 +298,20 @@ def _get_docstring_lines( return [] return [prepare_docstring(docstring, tab_width)] + if props.obj_type == 'type': + try: + analyzer = ModuleAnalyzer.for_module(props.module_name) + analyzer.analyze() + except PycodeError: + return None + + key = ('', props.name) + if key in analyzer.attr_docs: + if comment := list(analyzer.attr_docs[key]): + return [comment] + + return None + docstring = getdoc( obj, allow_inherited=inherit_docstrings, diff --git a/tests/roots/test-ext-autodoc/target/pep695.py b/tests/roots/test-ext-autodoc/target/pep695.py index 8aa30aaaaba..3e57ed4baf4 100644 --- a/tests/roots/test-ext-autodoc/target/pep695.py +++ b/tests/roots/test-ext-autodoc/target/pep695.py @@ -22,6 +22,8 @@ class Foo: type Pep695Alias = Foo """This is PEP695 type alias.""" +type Pep695AliasUndocumented = Foo + TypeAliasTypeExplicit = TypeAliasType('TypeAliasTypeExplicit', Foo) # NoQA: UP040 """This is an explicitly constructed typing.TypeAlias.""" diff --git a/tests/test_ext_autodoc/test_ext_autodoc.py b/tests/test_ext_autodoc/test_ext_autodoc.py index 03355d1f703..1d037288038 100644 --- a/tests/test_ext_autodoc/test_ext_autodoc.py +++ b/tests/test_ext_autodoc/test_ext_autodoc.py @@ -2292,6 +2292,12 @@ def test_autodoc_pep695_type_alias() -> None: ' This is PEP695 type alias of PEP695 alias.', '', '', + # Undocumented alias should not inherit any documentation + '.. py:type:: Pep695AliasUndocumented', + ' :module: target.pep695', + ' :canonical: ~target.pep695.Foo', + '', + '', '.. py:type:: Pep695AliasUnion', ' :module: target.pep695', ' :canonical: str | int',