Skip to content

Commit 707f210

Browse files
LecrisUTAA-Turner
andauthored
autodoc: Ensure type statements don't inherit docstrings (#14068)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
1 parent 22bb79e commit 707f210

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

sphinx/ext/autodoc/_docstrings.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _prepare_docstrings(
8888
) -> list[list[str]] | None:
8989
"""Add content from docstrings, attribute documentation and user."""
9090
# add content from attribute documentation
91-
if props.obj_type not in {'data', 'attribute'} and props.parts:
91+
if props.obj_type not in {'data', 'attribute', 'type'} and props.parts:
9292
key = ('.'.join(props.parent_names), props.name)
9393
try:
9494
# make a copy of docstring for attributes to avoid cache
@@ -298,6 +298,20 @@ def _get_docstring_lines(
298298
return []
299299
return [prepare_docstring(docstring, tab_width)]
300300

301+
if props.obj_type == 'type':
302+
try:
303+
analyzer = ModuleAnalyzer.for_module(props.module_name)
304+
analyzer.analyze()
305+
except PycodeError:
306+
return None
307+
308+
key = ('', props.name)
309+
if key in analyzer.attr_docs:
310+
if comment := list(analyzer.attr_docs[key]):
311+
return [comment]
312+
313+
return None
314+
301315
docstring = getdoc(
302316
obj,
303317
allow_inherited=inherit_docstrings,

tests/roots/test-ext-autodoc/target/pep695.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Foo:
2222
type Pep695Alias = Foo
2323
"""This is PEP695 type alias."""
2424

25+
type Pep695AliasUndocumented = Foo
26+
2527
TypeAliasTypeExplicit = TypeAliasType('TypeAliasTypeExplicit', Foo) # NoQA: UP040
2628
"""This is an explicitly constructed typing.TypeAlias."""
2729

tests/test_ext_autodoc/test_ext_autodoc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,12 @@ def test_autodoc_pep695_type_alias() -> None:
22922292
' This is PEP695 type alias of PEP695 alias.',
22932293
'',
22942294
'',
2295+
# Undocumented alias should not inherit any documentation
2296+
'.. py:type:: Pep695AliasUndocumented',
2297+
' :module: target.pep695',
2298+
' :canonical: ~target.pep695.Foo',
2299+
'',
2300+
'',
22952301
'.. py:type:: Pep695AliasUnion',
22962302
' :module: target.pep695',
22972303
' :canonical: str | int',

0 commit comments

Comments
 (0)