From dc541b9dcff1170a4fb0a5cb2b0da1f2930b5b46 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 8 Nov 2018 08:48:11 +0200 Subject: [PATCH] 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 a44d34e17908a49d584f86c4f8642a50707b7150) Co-authored-by: Serhiy Storchaka --- Lib/pydoc.py | 6 +- Lib/test/test_pydoc.py | 99 +++++++++++++++++++ .../2018-10-12-18-57-52.bpo-34966.WZeBHO.rst | 3 + 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-10-12-18-57-52.bpo-34966.WZeBHO.rst diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 8a6b27b16e5278..09992cd08245a2 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -958,8 +958,7 @@ def docroutine(self, object, name=None, mod=None, if name == realname: title = '%s' % (anchor, realname) else: - if (cl and realname in cl.__dict__ and - cl.__dict__[realname] is object): + if cl and inspect.getattr_static(cl, realname, []) is object: reallink = '%s' % ( cl.__name__ + '-' + realname, realname) skipdocs = 1 @@ -1375,8 +1374,7 @@ def docroutine(self, object, name=None, mod=None, cl=None): if name == realname: title = self.bold(realname) else: - if (cl and realname in cl.__dict__ and - cl.__dict__[realname] is object): + if cl and inspect.getattr_static(cl, realname, []) is object: skipdocs = 1 title = self.bold(name) + ' = ' + realname argspec = None diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index 06f872999515fb..9bf365ed4b95a9 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -417,6 +417,7 @@ def call_url_handler(self, url, expected_title): class PydocDocTest(unittest.TestCase): + maxDiff = None @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") @@ -648,6 +649,104 @@ def method_returning_true(self): methods = pydoc.allmethods(TestClass) self.assertDictEqual(methods, expected) + def test_method_aliases(self): + class A: + def tkraise(self, aboveThis=None): + """Raise this widget in the stacking order.""" + lift = tkraise + def a_size(self): + """Return size""" + class B(A): + def itemconfigure(self, tagOrId, cnf=None, **kw): + """Configure resources of an item TAGORID.""" + itemconfig = itemconfigure + b_size = A.a_size + + doc = pydoc.render_doc(B) + # clean up the extra text formatting that pydoc performs + doc = re.sub('\b.', '', doc) + self.assertEqual(doc, '''\ +Python Library Documentation: class B in module %s + +class B(A) + | Method resolution order: + | B + | A + | builtins.object + |\x20\x20 + | Methods defined here: + |\x20\x20 + | b_size = a_size(self) + |\x20\x20 + | itemconfig = itemconfigure(self, tagOrId, cnf=None, **kw) + |\x20\x20 + | itemconfigure(self, tagOrId, cnf=None, **kw) + | Configure resources of an item TAGORID. + |\x20\x20 + | ---------------------------------------------------------------------- + | Methods inherited from A: + |\x20\x20 + | a_size(self) + | Return size + |\x20\x20 + | lift = tkraise(self, aboveThis=None) + |\x20\x20 + | tkraise(self, aboveThis=None) + | Raise this widget in the stacking order. + |\x20\x20 + | ---------------------------------------------------------------------- + | Data descriptors inherited from A: + |\x20\x20 + | __dict__ + | dictionary for instance variables (if defined) + |\x20\x20 + | __weakref__ + | list of weak references to the object (if defined) +''' % __name__) + + doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc()) + self.assertEqual(doc, '''\ +Python Library Documentation: class B in module %s + +

+ + + +\x20\x20\x20\x20 + +
 
+class B(A)
    
Method resolution order:
+
B
+
A
+
builtins.object
+
+
+Methods defined here:
+
b_size = a_size(self)
+ +
itemconfig = itemconfigure(self, tagOrId, cnf=None, **kw)
+ +
itemconfigure(self, tagOrId, cnf=None, **kw)
Configure resources of an item TAGORID.
+ +
+Methods inherited from A:
+
a_size(self)
Return size
+ +
lift = tkraise(self, aboveThis=None)
+ +
tkraise(self, aboveThis=None)
Raise this widget in the stacking order.
+ +
+Data descriptors inherited from A:
+
__dict__
+
dictionary for instance variables (if defined)
+
+
__weakref__
+
list of weak references to the object (if defined)
+
+
\ +''' % __name__) + class PydocImportTest(PydocBaseTest): diff --git a/Misc/NEWS.d/next/Library/2018-10-12-18-57-52.bpo-34966.WZeBHO.rst b/Misc/NEWS.d/next/Library/2018-10-12-18-57-52.bpo-34966.WZeBHO.rst new file mode 100644 index 00000000000000..b861405297f4f6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-12-18-57-52.bpo-34966.WZeBHO.rst @@ -0,0 +1,3 @@ +:mod:`pydoc` now supports aliases not only to methods defined in +the end class, but also to inherited methods. The docstring is not +duplicated for aliases.