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.

Title: pydoc method documentation lookup enhancement
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: ping Nosy List: andyharrington, aschmolck, calvin, eric.araujo, isandler, ping, pitrou, ron_adam
Priority: normal Keywords: easy, patch

Created on 2004-10-02 09:39 by aschmolck, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit aschmolck, 2004-10-12 09:11
pydoc2.patch andyharrington, 2008-03-24 00:25 documentation 'inheritance' patch for pydoc, plus 4 supporting test files
Messages (12)
msg47005 - (view) Author: Alexander Schmolck (aschmolck) Date: 2004-10-02 09:39
If a method has no docstring, then pydoc.getdoc
currently just looks for a comment or returns nothing.
A more sensible behavior IMO would be if pydoc tried to
get the docstring from the same method in a baseclass
(in mro), if it exists.

This ought to be "the right thing" (i.e. by the
semantics of inheritance if the documentation of a
overriden method is not overriden it should still
present the same interface to the user) and the current
behavior is a royal pain when working with backends or
other types of subclasses that are derived from some
abstract class that contains all the interface docs.

Currently even fixing up docstrings by hand is not
simple, because the straightforward
``Child.meth.__doc__ = Parent.meth.__doc__`` fails (one
has to go directly via the .__dict__ which is problematic).
msg47006 - (view) Author: Ilya Sandler (isandler) Date: 2004-10-12 04:14
Logged In: YES 

Did you forget to attach the patch?

(SF does not show any attachments)
msg47007 - (view) Author: Alexander Schmolck (aschmolck) Date: 2004-10-12 09:11
Logged In: YES 

OK, another try...
msg62661 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-02-21 21:36
I agree this behaviour would definitely be useful.
msg64198 - (view) Author: Andy Harrington (andyharrington) Date: 2008-03-20 19:43
After going to the sprint Monday, I am working on this as my first patch.

There is no test file for pydoc.  ??
msg64349 - (view) Author: Andy Harrington (andyharrington) Date: 2008-03-22 23:38
Several points:

Additional note in pydoc output:

I thought that 'inherited' docs should be marked, so I chose to add to
the note for any function that gets docs displayed from an inherited
   ', docs from inherited <inherited class name>'
Alternate verbiage could certainly be used, but I strongly believe that
the substitution should be marked and its source indicated.  This
addition occurs in HTMLDoc.docroutine and TextDoc.docroutine.


If a user just calls help(<method reference>) or help(<class
reference>), this patch is certainly appropriate.  On the other hand, if
the call is help(<module reference>), and the module includes the
definitions of the inherited classes from which documentation is
'inherited', then there is duplication within the output, which the user
might or might not desire.  To allow a choice would require a change in
parameters or a global flag in pydoc.  A global flag like
allow_doc_redundancy could be set to False to block inherited
documentation when the ancestor function is already putting a copy of
the documentation in the output.  I would be happy to add this change if
requested, but I thought it would be presumptuous of me to just add it
to the requested patch.  


I added a test specifically for the inherited docs,
test.test_pydoc_inheritance, generating documentaton on 
test.test_pydoc_sample and comparing results to previously generated
results files test/test_pydoc.txt and test/test_pydoc.html.  If further
changes to pydoc cause outward change to the documentation format, run
test_pydoc_inheritance.regenerateData() to replace the result files.

Nothing in the patch in 2.6 dependent.  Users could choose to upgrade
pydoc for 2.5.x
msg64385 - (view) Author: Andy Harrington (andyharrington) Date: 2008-03-24 00:25
HM, before writing my patch I tested pydoc to see the issue was still
there.  I did not look at the 2004 patch from aschmolck since it was so
old and was clearly not implemented, and brett just listed this issue as
one to deal with in 2008.  Now I see does address the
same issue.

Comments on the differences:
1.  I allow for the case that an ancestor uses the name but not as a
method.  That *should* stop the search.

2.  The 2004 patch does not use inspect.ismethod, but creates its own test. 

3.  I stuck with the original pydoc convention that comments could
substitute for docs.  The 2004 patch does not look for comments in
ancestors and only uses comments in the current method if no ancestor
has docs.  That is a difference in design that could be discussed.  I am
OK with either.

4.  The 2004 patch makes its substitution silently.  I prefer explicitly
noting that the docs are 'inherited'.

5.  There is nothing to add to the test package in the 2004 patch.

Before looking at the 2004 patch, I replaced my last patch.  I just
reread the Python source and documentation conventions and changed names
and documentation to match.  

The only change to my previous comments is that 
was renamed

One related comment after thinking about the style guides:  Should this
pydoc change affect the style guide?  Is duplication in the source code
recommended for 'inherited' docs?  Rather than say absolutely nothing in
the overriding method, would a standard comment 
#inherit docs
make sense in the source code?  In that case a further change to pydoc
is needed to recognize this as a special case, where the 'inherited'
docs should be substituted.  Alternately the search sequence followed in
the 2004 patch could be used, which would find the inherited docs before
the comment, whatever the comment.
msg64387 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-03-24 00:35
Andy, I haven't looked at your patch in great detail but your approach
sounds good to me. Probably other people will want to study it and pick
on details :)

I don't think the "#inherit docs" proposal brings any added value over
the straightforward approach taken in your patch. The fact that the
documentation output states when the docstring is inherited seems clear
msg64392 - (view) Author: Alexander Schmolck (aschmolck) Date: 2008-03-24 04:06
Ah, well nice to see this finally going somewhere, although I'm a bit
puzzled as to why my patch was "clearly not implemented" :)

Andy, wrt. to your points:
1.  Yes, but see below.

2. Are you sure using inspect.ismethod is an improvement? I'm pretty
sure I was aware of it and didn't use it for a reason -- from a
superficial glance it appears to me that inspect.getmethod is just
broken for this purpose. Or do you have a good reason why you'd like to
exclude e.g. methods inherited from a builtin? I have no time to check
right now, maybe the behavior of ismethod has changed or I remember it
wrongly, but in general I think doc-lookup should be oblivious to
changes that are transparent at the interface level and whether
something is inherited from a builtin or not is should be considered as
a mere implementation detail and not at all affect the documentation lookup.

3. I don't feel strongly about this but I'm personally not that keen on
using comments as a substitute for docs -- I see no point in conflating
these two mechanisms which serve quite different purposes
(implementation elucidation vs interface description), especially if the
comment is taken from *some other implementation*.

4. Certainly fine by me.

msg64399 - (view) Author: Andy Harrington (andyharrington) Date: 2008-03-24 08:17

I have no idea why your patch languished.  On the one hand I might have
skipped this if I realized that before.  On the other hand, I did add
something extra, and I might not have had an open mind if I had looked
at yours.  Plus, maybe I got it moving! :)  You made a good enhancement
suggestion.  From the continuing interchange below, it looks like two
minds are better than either one.

Comments on your comments on mine:

2.  Hm, good point.  I do not think pydoc should substitute for PyLint,
but it only makes sense to copy the docs from the same type of function
in an ancestor class: static, class or method, though you are right it
need not matter whether the ancestor is built-in or not, though the
assumption is made that you know about built-in docs - you might only
note the inheritance.  

And again, as I did it, finding any ancestor of the wrong type should
stop the search.

There is one inconsistency with static method docs:  If we are given a
class, and have a static method in it, we can search the inheritance
chain for docs.  Neither of us does this at present, though it looks
easier to add through my access point in docroutine.  On the other hand,
if the top-level request is just to document a static function, there is
a problem, since you cannot identify the class it comes from, not having
an im_class attribute, and hence the documentation may look different
from when it was just a part of the documentation for the class.  I do
not see this as a reason to skip the inheritance chain for a static
method when given it's class: better information in -> better
information out.

3.  I agree people should use doc strings not comments, but if everyone
did that, our versions would have the same effect.  The only difference
is if the coder *does* want to use comments for some reason.  Hence the
question is: do you want to document what people do or push them to code
the way you want?  Ping apparently chose the former approach, so I am
not suggesting changing it.   

msg127828 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-02-03 22:56
Note that similar (duplicate?) #426740 was rejected, so maybe the discussion here is moot.

FWIW, I believe using a decorator or a custom metaclass to explicitly inherit a docstring is a good way of achieving what you want.
msg127854 - (view) Author: Ron Adam (ron_adam) * Date: 2011-02-04 01:38
I agree. It is close enough to be a duplicate. I suggest closing it.

As Ka-Ping noted in the other issue:
"There's a link to the base class provided if you want to find out what the base class does."

This is easy to do if your viewing pydoc output in a web browser.  And you can also look at the source code to see any comments.

At some point we can consider enhancing the command line help mode to make it easier to do the same.
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 40974
2011-03-15 03:41:30brian.curtinsetstatus: open -> closed
nosy: ping, calvin, isandler, aschmolck, pitrou, andyharrington, ron_adam, eric.araujo
resolution: rejected
stage: resolved
2011-02-04 01:38:16ron_adamsetnosy: ping, calvin, isandler, aschmolck, pitrou, andyharrington, ron_adam, eric.araujo
messages: + msg127854
2011-02-03 22:56:45eric.araujosetnosy: + calvin

messages: + msg127828
versions: + Python 3.3, - Python 3.2
2010-08-19 16:41:26BreamoreBoysetnosy: + ron_adam
2010-08-07 18:11:16terry.reedysetversions: + Python 3.2, - Python 2.6
2010-06-02 08:39:01eric.araujosetnosy: + eric.araujo
2008-03-24 08:18:01andyharringtonsetmessages: + msg64399
2008-03-24 04:06:30aschmolcksetmessages: + msg64392
2008-03-24 00:35:09pitrousetmessages: + msg64387
2008-03-24 00:25:15andyharringtonsetfiles: + pydoc2.patch
messages: + msg64385
2008-03-23 23:40:54andyharringtonsetfiles: - pydoc.PATCH
2008-03-22 23:38:45andyharringtonsetfiles: + pydoc.PATCH
messages: + msg64349
2008-03-20 19:43:31andyharringtonsetnosy: + andyharrington
messages: + msg64198
2008-02-21 21:38:04pitrousettype: enhancement
versions: + Python 2.6, - Python 2.4
2008-02-21 21:36:47pitrousetnosy: + pitrou
messages: + msg62661
2008-02-19 23:34:28akuchlingsetkeywords: + easy
2004-10-02 09:39:34aschmolckcreate