Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion sphinx/ext/autodoc/_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/roots/test-ext-autodoc/target/pep695.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
6 changes: 6 additions & 0 deletions tests/test_ext_autodoc/test_ext_autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading