From 54765d4b0d156a44faf3f9256dd9419da5e6eba1 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 12 Oct 2018 18:54:48 +0300 Subject: [PATCH 1/3] bpo-34966: Improve support of method aliases in pydoc. Pydoc now detects aliases to inherited methods. --- Lib/pydoc.py | 6 +- Lib/test/test_pydoc.py | 99 +++++++++++++++++++ .../2018-10-12-18-57-52.bpo-34966.WZeBHO.rst | 2 + 3 files changed, 103 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..726bae631e4170 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 + | + | 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__) + + doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc()) + self.assertEqual(doc, '''\ +Python Library Documentation: class B in module %s + +

+ + + + + +
 
+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..560c4f527cc27e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-12-18-57-52.bpo-34966.WZeBHO.rst @@ -0,0 +1,2 @@ +:mod:`pydoc` now supports aliases to inherited methods, not just to defined +in the end class. From f79d727a59cf24dd67f458152449f45b6f24e88a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 21 Oct 2018 15:54:02 +0300 Subject: [PATCH 2/3] Escape trailing spaces. --- Lib/test/test_pydoc.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index d5d53528f90e7a..d521deda176002 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -791,33 +791,33 @@ class B(A) | 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__) @@ -831,7 +831,7 @@ class B(A)  
class B(A) - +\x20\x20\x20\x20     

Method resolution order:
B
From b292aedd8e5c2a3e51a96501e8d32968849d006b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 29 Oct 2018 20:32:41 +0200 Subject: [PATCH 3/3] Update 2018-10-12-18-57-52.bpo-34966.WZeBHO.rst --- .../next/Library/2018-10-12-18-57-52.bpo-34966.WZeBHO.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 index 560c4f527cc27e..b861405297f4f6 100644 --- 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 @@ -1,2 +1,3 @@ -:mod:`pydoc` now supports aliases to inherited methods, not just to defined -in the end class. +: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.