Skip to content
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

Feature request: Option to accept inherited docstrings #12

Open
1 of 2 tasks
torfsen opened this issue May 5, 2020 · 2 comments
Open
1 of 2 tasks

Feature request: Option to accept inherited docstrings #12

torfsen opened this issue May 5, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@torfsen
Copy link

torfsen commented May 5, 2020

Is this a BUG REPORT or FEATURE REQUEST?:

  • bug
  • feature

Description of Bug or Feature

Many tools (e.g. inspect.getdoc, Sphinx) allow methods in subclasses to inherit the docstring of the parent method:

class Foo:
    def do_it(self):
        """
        Do the thing!
        """
        pass

class SpecialFoo(Foo):
    def do_it(self):
        print('So special!')

In these cases, I usually only add a separate docstring to SpecialFoo.do_it if its behavior differs significantly from what one would expected after reading the docstring of Foo.do_it. In most cases, the docstring of Foo.do_it is just fine.

Hence I'd like an option to not count these cases as missing.

@econchick
Copy link
Owner

Hmm interesting thought. I'm going to have to investigate this a bit; it's weird that help(ChildClass.overwritten_method) shows the docstring of the parent class, but ast.get_docstring does not.

I'll get back to you - thanks for the idea!

@econchick econchick added the enhancement New feature or request label May 6, 2020
@J08nY
Copy link

J08nY commented Jan 20, 2021

Hmm interesting thought. I'm going to have to investigate this a bit; it's weird that help(ChildClass.overwritten_method) shows the docstring of the parent class, but ast.get_docstring does not.

The way help() works is using pydoc which does its own search of the doc string. See: https://github.com/python/cpython/blob/63298930fb531ba2bb4f23bc3b915dbf1e17e9e1/Lib/pydoc.py#L185. There getdoc is called with the class as an argument. That further calls _getdoc which first tries _getowndoc which finds the docstring on the class itself and then _finddoc which looks through the superclasses in MRO (here) of the class and returns something. This could be also done in interrogate.

EDIT: Oh, so it seems this might not be so easy to add, as interrogate works on the ast level and does not really have access to the fully initialized classes, only their ast. To work it would have to somehow build the list of superclasses of a given class purely from the ast nodes it has seen (so this would likely need to be done as some sort of a post-processing step, after all nodes were processed), and then also include docstrings from classes outside of the analyzed code (libraries and stdlib) which would likely need to be imported. And in general this whole thing gets really tricky when working purely on the ast level, as the full superclass information is simply not there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants