mkdocstrings / pytkdocs

Load Python objects documentation.
https://mkdocstrings.github.io/pytkdocs
ISC License
50 stars 32 forks source link

[BUG] Properties do not inherit superclass docstrings #90

Closed jayqi closed 3 years ago

jayqi commented 3 years ago

Describe the bug

Properties do not properly inherit docstrings from parent class if they are not set.

To Reproduce

I observed this when rendering documentation using mkdocstrings.

From looking at the pytokdocs source code, I think the problem is because the get_property_documentation method is using inspect.getdoc on prop.fget instead of prop directly (link).

Based on my experimentation below, both the property itself and fget work for the superclass but only the property and not fget work the subclass.

import inspect

class SuperClass:
    @property
    def read_only(self):
        """SuperClass.read_only docs"""
        return 0

    @property
    def mutable(self):
        """SuperClass.mutable getter docs"""
        return 0

    @mutable.setter
    def mutable(self, value):
        pass

class SubClass(SuperClass):
    @property
    def read_only(self):
        return 1

    @property
    def mutable(self):
        return 1

    @mutable.setter
    def mutable(self, value):
        pass

inspect.getdoc(SuperClass.read_only)
#> 'SuperClass.read_only docs'

inspect.getdoc(SuperClass.read_only.fget)
#> 'SuperClass.read_only docs'

inspect.getdoc(SuperClass.mutable)
#> 'SuperClass.mutable getter docs'

inspect.getdoc(SuperClass.mutable.fget)
#> 'SuperClass.mutable getter docs'

inspect.getdoc(SubClass.read_only)
#> 'SuperClass.read_only docs'

inspect.getdoc(SubClass.read_only.fget)

inspect.getdoc(SubClass.mutable)
#> 'SuperClass.mutable getter docs'

inspect.getdoc(SubClass.mutable.fget)

Created on 2021-02-05 by the reprexpy package

Expected behavior

Sub class properties should inherit docstrings from their parents when not set.

System (please complete the following information):

pawamoy commented 3 years ago

Hi @jayqi! Thanks a lot for the detailed report and the examples. I'll see if we can use getdoc on the property itself rather than its fget method.

pawamoy commented 3 years ago

Pushed a fix, will release it soon, closing!