Skip to content

Commit 11a33e1

Browse files
bpo-34966: Improve support of method aliases in pydoc. (GH-9823)
Pydoc now does not duplicate docstrings for aliases of inherited methods. (cherry picked from commit a44d34e) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent de25071 commit 11a33e1

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

‎Lib/pydoc.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,7 @@ def docroutine(self, object, name=None, mod=None,
941941
if name == realname:
942942
title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname)
943943
else:
944-
if (cl and realname in cl.__dict__ and
945-
cl.__dict__[realname] is object):
944+
if cl and inspect.getattr_static(cl, realname, []) is object:
946945
reallink = '<a href="#%s">%s</a>' % (
947946
cl.__name__ + '-' + realname, realname)
948947
skipdocs = 1
@@ -1346,8 +1345,7 @@ def docroutine(self, object, name=None, mod=None, cl=None):
13461345
if name == realname:
13471346
title = self.bold(realname)
13481347
else:
1349-
if (cl and realname in cl.__dict__ and
1350-
cl.__dict__[realname] is object):
1348+
if cl and inspect.getattr_static(cl, realname, []) is object:
13511349
skipdocs = 1
13521350
title = self.bold(name) + ' = ' + realname
13531351
argspec = None

‎Lib/test/test_pydoc.py‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ def call_url_handler(self, url, expected_title):
419419

420420

421421
class PydocDocTest(unittest.TestCase):
422+
maxDiff = None
422423

423424
@unittest.skipIf(sys.flags.optimize >= 2,
424425
"Docstrings are omitted with -O2 and above")
@@ -650,6 +651,104 @@ def method_returning_true(self):
650651
methods = pydoc.allmethods(TestClass)
651652
self.assertDictEqual(methods, expected)
652653

654+
def test_method_aliases(self):
655+
class A:
656+
def tkraise(self, aboveThis=None):
657+
"""Raise this widget in the stacking order."""
658+
lift = tkraise
659+
def a_size(self):
660+
"""Return size"""
661+
class B(A):
662+
def itemconfigure(self, tagOrId, cnf=None, **kw):
663+
"""Configure resources of an item TAGORID."""
664+
itemconfig = itemconfigure
665+
b_size = A.a_size
666+
667+
doc = pydoc.render_doc(B)
668+
# clean up the extra text formatting that pydoc performs
669+
doc = re.sub('\b.', '', doc)
670+
self.assertEqual(doc, '''\
671+
Python Library Documentation: class B in module %s
672+
673+
class B(A)
674+
| Method resolution order:
675+
| B
676+
| A
677+
| builtins.object
678+
|\x20\x20
679+
| Methods defined here:
680+
|\x20\x20
681+
| b_size = a_size(self)
682+
|\x20\x20
683+
| itemconfig = itemconfigure(self, tagOrId, cnf=None, **kw)
684+
|\x20\x20
685+
| itemconfigure(self, tagOrId, cnf=None, **kw)
686+
| Configure resources of an item TAGORID.
687+
|\x20\x20
688+
| ----------------------------------------------------------------------
689+
| Methods inherited from A:
690+
|\x20\x20
691+
| a_size(self)
692+
| Return size
693+
|\x20\x20
694+
| lift = tkraise(self, aboveThis=None)
695+
|\x20\x20
696+
| tkraise(self, aboveThis=None)
697+
| Raise this widget in the stacking order.
698+
|\x20\x20
699+
| ----------------------------------------------------------------------
700+
| Data descriptors inherited from A:
701+
|\x20\x20
702+
| __dict__
703+
| dictionary for instance variables (if defined)
704+
|\x20\x20
705+
| __weakref__
706+
| list of weak references to the object (if defined)
707+
''' % __name__)
708+
709+
doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc())
710+
self.assertEqual(doc, '''\
711+
Python Library Documentation: class B in module %s
712+
713+
<p>
714+
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
715+
<tr bgcolor="#ffc8d8">
716+
<td colspan=3 valign=bottom>&nbsp;<br>
717+
<font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(A)</font></td></tr>
718+
\x20\x20\x20\x20
719+
<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
720+
<td width="100%%"><dl><dt>Method resolution order:</dt>
721+
<dd>B</dd>
722+
<dd>A</dd>
723+
<dd><a href="builtins.html#object">builtins.object</a></dd>
724+
</dl>
725+
<hr>
726+
Methods defined here:<br>
727+
<dl><dt><a name="B-b_size"><strong>b_size</strong></a> = <a href="#B-a_size">a_size</a>(self)</dt></dl>
728+
729+
<dl><dt><a name="B-itemconfig"><strong>itemconfig</strong></a> = <a href="#B-itemconfigure">itemconfigure</a>(self, tagOrId, cnf=None, **kw)</dt></dl>
730+
731+
<dl><dt><a name="B-itemconfigure"><strong>itemconfigure</strong></a>(self, tagOrId, cnf=None, **kw)</dt><dd><tt>Configure&nbsp;resources&nbsp;of&nbsp;an&nbsp;item&nbsp;TAGORID.</tt></dd></dl>
732+
733+
<hr>
734+
Methods inherited from A:<br>
735+
<dl><dt><a name="B-a_size"><strong>a_size</strong></a>(self)</dt><dd><tt>Return&nbsp;size</tt></dd></dl>
736+
737+
<dl><dt><a name="B-lift"><strong>lift</strong></a> = <a href="#B-tkraise">tkraise</a>(self, aboveThis=None)</dt></dl>
738+
739+
<dl><dt><a name="B-tkraise"><strong>tkraise</strong></a>(self, aboveThis=None)</dt><dd><tt>Raise&nbsp;this&nbsp;widget&nbsp;in&nbsp;the&nbsp;stacking&nbsp;order.</tt></dd></dl>
740+
741+
<hr>
742+
Data descriptors inherited from A:<br>
743+
<dl><dt><strong>__dict__</strong></dt>
744+
<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
745+
</dl>
746+
<dl><dt><strong>__weakref__</strong></dt>
747+
<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
748+
</dl>
749+
</td></tr></table>\
750+
''' % __name__)
751+
653752

654753
class PydocImportTest(PydocBaseTest):
655754

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`pydoc` now supports aliases not only to methods defined in
2+
the end class, but also to inherited methods. The docstring is not
3+
duplicated for aliases.

0 commit comments

Comments
 (0)