Skip to content

Commit 5018422

Browse files
authored
Merge pull request #7136 from tk0miya/5673_nested_classes
Fix incorrect handling of nested class names
2 parents 4c56beb + bc85f5a commit 5018422

File tree

8 files changed

+46
-2
lines changed

8 files changed

+46
-2
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Bugs fixed
5858
declarations.
5959
* C++, suppress warnings for directly dependent typenames in cross references
6060
generated automatically in signatures.
61+
* #5637: autodoc: Incorrect handling of nested class names on show-inheritance
62+
* #5637: inheritance_diagram: Incorrect handling of nested class names
6163

6264
Testing
6365
--------

sphinx/ext/autodoc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ def add_directive_header(self, sig: str) -> None:
11461146
if hasattr(self.object, '__bases__') and len(self.object.__bases__):
11471147
bases = [':class:`%s`' % b.__name__
11481148
if b.__module__ in ('__builtin__', 'builtins')
1149-
else ':class:`%s.%s`' % (b.__module__, b.__name__)
1149+
else ':class:`%s.%s`' % (b.__module__, b.__qualname__)
11501150
for b in self.object.__bases__]
11511151
self.add_line(' ' + _('Bases: %s') % ', '.join(bases),
11521152
sourcename)

sphinx/ext/inheritance_diagram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def class_name(self, cls: Any, parts: int = 0, aliases: Dict[str, str] = None) -
229229
if module in ('__builtin__', 'builtins'):
230230
fullname = cls.__name__
231231
else:
232-
fullname = '%s.%s' % (module, cls.__name__)
232+
fullname = '%s.%s' % (module, cls.__qualname__)
233233
if parts == 0:
234234
result = fullname
235235
else:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def meth(self):
110110
factory = dict
111111

112112

113+
class InnerChild(Outer.Inner):
114+
"""InnerChild docstring"""
115+
116+
113117
class DocstringSig(object):
114118
def meth(self):
115119
"""meth(FOO, BAR=1) -> BAZ
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Diagram with Nested Classes
2+
===========================
3+
4+
.. inheritance-diagram::
5+
dummy.test_nested
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Test with nested classes.
3+
"""
4+
5+
6+
class A(object):
7+
class B(object):
8+
pass
9+
10+
11+
class C(A.B):
12+
pass

tests/test_autodoc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ def test_autodoc_ignore_module_all(app):
685685
assert list(filter(lambda l: 'class::' in l, actual)) == [
686686
'.. py:class:: Class(arg)',
687687
'.. py:class:: CustomDict',
688+
'.. py:class:: InnerChild',
688689
'.. py:class:: InstAttCls()',
689690
'.. py:class:: Outer',
690691
' .. py:class:: Outer.Inner',
@@ -775,6 +776,18 @@ def test_autodoc_inner_class(app):
775776
' ',
776777
]
777778

779+
options['show-inheritance'] = True
780+
actual = do_autodoc(app, 'class', 'target.InnerChild', options)
781+
assert list(actual) == [
782+
'',
783+
'.. py:class:: InnerChild',
784+
' :module: target', '',
785+
' Bases: :class:`target.Outer.Inner`',
786+
'',
787+
' InnerChild docstring',
788+
' '
789+
]
790+
778791

779792
@pytest.mark.sphinx('html', testroot='ext-autodoc')
780793
def test_autodoc_classmethod(app):

tests/test_ext_inheritance_diagram.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def new_run(self):
132132
('dummy.test.A', 'dummy.test.A', [], None),
133133
]
134134

135+
# inheritance diagram involving a base class nested within another class
136+
for cls in graphs['diagram_w_nested_classes'].class_info:
137+
assert cls in [
138+
('dummy.test_nested.A', 'dummy.test_nested.A', [], None),
139+
('dummy.test_nested.C', 'dummy.test_nested.C', ['dummy.test_nested.A.B'], None),
140+
('dummy.test_nested.A.B', 'dummy.test_nested.A.B', [], None)
141+
]
142+
135143

136144
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram')
137145
@pytest.mark.usefixtures('if_graphviz_found')

0 commit comments

Comments
 (0)