Skip to content

Commit 766900e

Browse files
pholicaAA-Turner
andauthored
autodoc: Handle multiple inheritance correctly (#13136)
Co-authored-by: Adam Turner <[email protected]>
1 parent 774f8cc commit 766900e

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Bugs fixed
134134
* #12975: Avoid rendering a trailing comma in C and C++ multi-line signatures.
135135
* #13178: autodoc: Fix resolution for ``pathlib`` types.
136136
Patch by Adam Turner.
137+
* #13136: autodoc: Correctly handle multiple inheritance.
138+
Patch by Pavel Holica
137139

138140
Testing
139141
-------

sphinx/ext/autodoc/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,10 +742,19 @@ def filter_members(
742742

743743
def is_filtered_inherited_member(name: str, obj: Any) -> bool:
744744
inherited_members = self.options.inherited_members or set()
745+
seen = set()
745746

746747
if inspect.isclass(self.object):
747748
for cls in self.object.__mro__:
748-
if cls.__name__ in inherited_members and cls != self.object:
749+
if name in cls.__dict__:
750+
seen.add(cls)
751+
if (
752+
cls.__name__ in inherited_members
753+
and cls != self.object
754+
and any(
755+
issubclass(potential_child, cls) for potential_child in seen
756+
)
757+
):
749758
# given member is a member of specified *super class*
750759
return True
751760
if name in cls.__dict__:

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ def inheritedstaticmeth(cls): # NoQA: PLW0211
1414
"""Inherited static method."""
1515

1616

17-
class Derived(Base):
17+
class AnotherBase:
18+
#: docstring
19+
def another_inheritedmeth(self):
20+
"""Another inherited function."""
21+
22+
23+
class Derived(Base, AnotherBase):
1824
def inheritedmeth(self):
1925
# no docstring here
2026
pass

tests/test_extensions/test_ext_autodoc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ def test_autodoc_inherited_members(app):
831831
}
832832
actual = do_autodoc(app, 'class', 'target.inheritance.Derived', options)
833833
assert list(filter(lambda l: 'method::' in l, actual)) == [
834+
' .. py:method:: Derived.another_inheritedmeth()',
834835
' .. py:method:: Derived.inheritedclassmeth()',
835836
' .. py:method:: Derived.inheritedmeth()',
836837
' .. py:method:: Derived.inheritedstaticmeth(cls)',

tests/test_extensions/test_ext_autodoc_automodule.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ def test_automodule_inherited_members(app):
134134
'.. py:module:: target.inheritance',
135135
'',
136136
'',
137+
'.. py:class:: AnotherBase()',
138+
' :module: target.inheritance',
139+
'',
140+
'',
141+
' .. py:method:: AnotherBase.another_inheritedmeth()',
142+
' :module: target.inheritance',
143+
'',
144+
' Another inherited function.',
145+
'',
146+
'',
137147
'.. py:class:: Base()',
138148
' :module: target.inheritance',
139149
'',
@@ -169,6 +179,12 @@ def test_automodule_inherited_members(app):
169179
' :module: target.inheritance',
170180
'',
171181
'',
182+
' .. py:method:: Derived.another_inheritedmeth()',
183+
' :module: target.inheritance',
184+
'',
185+
' Another inherited function.',
186+
'',
187+
'',
172188
' .. py:method:: Derived.inheritedmeth()',
173189
' :module: target.inheritance',
174190
'',

tests/test_extensions/test_ext_autodoc_configs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ def test_autodoc_inherit_docstrings_for_inherited_members(app):
319319
' :module: target.inheritance',
320320
'',
321321
'',
322+
' .. py:method:: Derived.another_inheritedmeth()',
323+
' :module: target.inheritance',
324+
'',
325+
' Another inherited function.',
326+
'',
327+
'',
322328
' .. py:attribute:: Derived.inheritedattr',
323329
' :module: target.inheritance',
324330
' :value: None',
@@ -356,6 +362,12 @@ def test_autodoc_inherit_docstrings_for_inherited_members(app):
356362
' :module: target.inheritance',
357363
'',
358364
'',
365+
' .. py:method:: Derived.another_inheritedmeth()',
366+
' :module: target.inheritance',
367+
'',
368+
' Another inherited function.',
369+
'',
370+
'',
359371
' .. py:method:: Derived.inheritedclassmeth()',
360372
' :module: target.inheritance',
361373
' :classmethod:',

0 commit comments

Comments
 (0)