Skip to content

Share code for getdoc() between inspect and pydoc #132686

Closed

Description

@serhiy-storchaka

Currently, inspect and pydoc use similar, but slightly different complex code to retrieve a doc string for object (class, function, method, etc). This is a consequence of two issues: bpo-15582 (#59787) and bpo-40257 (#84438).

In #59787, inheriting docstrings from parent class was added. It usually works fine for overridden methods, but for classes it is often wrong. If the parent class docstring says that it is an abstract class, a base class or a generic implementation class, showing this for a subclass is wrong. It is worse than not showing a docstring at all (like it was before #59787). So #84438 reverted this particular change. It fixed also other issue -- pydoc now shows a docstring not only for classes and function, but for arbitrary objects if they have docstring. This allows to show docstring for typing.Any and other pseudo-types. But there were complains against these changes, so they were reverted for inspect.getdoc(). But taht behavior is wrong for pydoc, so a modified version of inspect.getdoc() was copied in pydoc.

The problem is that that code is pretty complex. And any time we change it (for example #131116), we need to change in two places. It is easy to forgot to synchronize the code or introduce a bug. Also, the third-party code that use getdoc() may need the behavior of the pydoc version, which is not public.

The implementation should be shared. We may add options to control the behavior, and the default behavior may change in future. If there is a way to get the old behavior, there would be less complains.

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 18, 2025
  2. thomashammett commented on Apr 18, 2025

    @thomashammett

    I'd like to work on this issue and submit a PR to fix it. Is anyone already working on this, or may I take it? Thank you.

  3. added a commit that references this issue on Apr 18, 2025
  4. serhiy-storchaka commented on Apr 18, 2025

    @serhiy-storchaka
    MemberAuthor

    #132691 adds new options for getdoc().

    @ncoghlan, @vadmium, @vedgar, @Carreau, @gvanrossum, what are your thoughts about these options?

  5. serhiy-storchaka commented on Apr 18, 2025

    @serhiy-storchaka
    MemberAuthor

    Sorry, @thomashammett, the work was already almost done when I created this issue.

  6. thomashammett commented on Apr 18, 2025

    @thomashammett

    No worries! Thanks.

  7. vedgar commented on Apr 20, 2025

    @vedgar
    Contributor

    #132691 adds new options for getdoc().

    @ncoghlan, @vadmium, @vedgar, @Carreau, @gvanrossum, what are your thoughts about these options?

    I'm not quite sure why I was mentioned here... is there a particular reason you want my opinion?

  8. serhiy-storchaka commented on Apr 20, 2025

    @serhiy-storchaka
    MemberAuthor

    Because you were one of the opponents of unconditional change in #84438.
    #84438 (comment)
    #84438 (comment)

  9. vedgar commented on Apr 20, 2025

    @vedgar
    Contributor
  10. added a commit that references this issue on Nov 11, 2025
  11. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions