Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
serhiy-storchaka opened this issue Apr 18, 2025 · 7 comments
Open

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

serhiy-storchaka opened this issue Apr 18, 2025 · 7 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Apr 18, 2025

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

@picnixz picnixz added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Apr 18, 2025
@thomashammett
Copy link

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.

@serhiy-storchaka
Copy link
Member Author

#132691 adds new options for getdoc().

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

@serhiy-storchaka
Copy link
Member Author

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

@thomashammett
Copy link

No worries! Thanks.

@vedgar
Copy link
Contributor

vedgar commented Apr 20, 2025

#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?

@serhiy-storchaka
Copy link
Member Author

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

@vedgar
Copy link
Contributor

vedgar commented Apr 20, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants