This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients Aaron Hall, JJeffries, eric.araujo, ezio.melotti, pconnell, rhettinger, xtreak
Date 2018-11-06.07:53:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1541490800.18.0.788709270274.issue12154@psf.upfronthosting.co.za>
In-reply-to
Content
Adding my analysis here which is also at related issue : issue30129. On the attached program written by @skip.montanaro both c.sum and Child.sum return None for inspect.getdocs thus docstrings in pydoc are empty which can be fixed in both functools and inspect module as below : 

1. When we do inspect.getdoc(c.sum) where c is a partialmethod it looks for obj.__doc__ (c.sum.__doc__) returning partialmethod.__doc__ ("new function with partial...") instead of checking if c.sum is a partial method and  returning obj.func.__doc__ which contains the relevant doc ("sum doc"). Thus getdoc needs to check before trying for obj.__doc__ if the obj is a partialmethod or partial object thus returning the original function object.

2. When we do inspect.getdoc(Child.sum) it looks for obj.__doc__ (Child.sum.__doc__) and since Child.sum is a partialmethod which has __get__ overridden it calls _make_unbound_method that returns a _method object with no doc and thus returning None. partialmethod object copies objects from the given function at [0] and the actual object is returned at [1] . Here self.func has the original function in this case Base.sum and _method._partialmethod has reference to Base.sum which contains the relevant docs but _method itself has no docs thus pydoc doesn't get any docs. So we can set _method.__doc__ = self.func.__doc__ and getdoc can pick up the docs.

[0] https://github.com/python/cpython/blob/f1b9ad3d38c11676b45edcbf2369239bae436e56/Lib/functools.py#L368
[1] https://github.com/python/cpython/blob/f1b9ad3d38c11676b45edcbf2369239bae436e56/Lib/functools.py#L401

Before patch partialmethod.__doc__ : 

$ ./python.exe
>>> import functools, inspect
>>> inspect.getdoc(functools.partial(int, base=2))
'partial(func, *args, **keywords) - new function with partial application\nof the given arguments and keywords.'

After patch returns int.__doc__ : 

./python.exe
>>> import functools, inspect
>>> inspect.getdoc(functools.partial(int, base=2))
"int([x]) -> integer\nint(x, base=10) -> integer\n\nConvert a number or string to an integer ..." # Trimmed

# Patch

diff --git a/Lib/functools.py b/Lib/functools.py
index ab7d71e126..751f67fcd0 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -398,6 +398,7 @@ class partialmethod(object):
             return self.func(*call_args, **call_keywords)
         _method.__isabstractmethod__ = self.__isabstractmethod__
         _method._partialmethod = self
+        _method.__doc__ = self.func.__doc__ or self.__doc__
         return _method

     def __get__(self, obj, cls):
diff --git a/Lib/inspect.py b/Lib/inspect.py
index b8a142232b..2c796546b2 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -600,6 +600,9 @@ def getdoc(object):
     All tabs are expanded to spaces.  To clean up docstrings that are
     indented to line up with blocks of code, any whitespace than can be
     uniformly removed from the second line onwards is removed."""
+    if isinstance(object, (functools.partialmethod, functools.partial)):
+        return object.func.__doc__
+
     try:
         doc = object.__doc__
     except AttributeError:
History
Date User Action Args
2018-11-06 07:53:20xtreaksetrecipients: + xtreak, rhettinger, ezio.melotti, eric.araujo, JJeffries, pconnell, Aaron Hall
2018-11-06 07:53:20xtreaksetmessageid: <1541490800.18.0.788709270274.issue12154@psf.upfronthosting.co.za>
2018-11-06 07:53:20xtreaklinkissue12154 messages
2018-11-06 07:53:20xtreakcreate