mkdocstrings / python

A Python handler for mkdocstrings.
https://mkdocstrings.github.io/python
ISC License
190 stars 35 forks source link

feature: Add option s.t. overridden members are able to "inherit" docstrings from corresponding members in parent classes #194

Closed thomasmarwitz closed 1 month ago

thomasmarwitz commented 1 month ago

Is your feature request related to a problem? Please describe.

This problem occurred during the migration from sphinx to mkdocs, specifically when auto-generating the API documentation ^1.

In sphinx, it is possible (apparently the default when turning autodoc_default_options = { "inherited-members": True, ... } on) to have a docstring of a method in a parent class appear in the documentation of a subclass that overrides this method without supplying a new docstring.

Example^2:

class Shape:
    contour: list[Point]
    def surface_area(self):
        """Return the surface area in square meters."""
        return numerical_integration(self.contour)

class Rectange(Shape)
    def surface_area(self):
        return distance(self.cotour[2], self.contour[0]) * distance(self.contour[3], self.contour[1])

We would like to have "Return the surface area in square meters." also appear for surface_area.

Additionally, sphinx performs some merging of docstrings[^3], as demonstrated below. Example:

class Shape:
    contour: list[Point]
    def surface_area(self):
        """Return the surface area in square meters."""
        return numerical_integration(self.contour)

class Rectange(Shape)
    def surface_area(self):
        """Note: This is way faster than the calculation for general shapes!"""
        return distance(self.cotour[2], self.contour[0]) * distance(self.contour[3], self.contour[1])

For this example, the documentation for surface_area should include: "Return the surface area in square meters. Note: This is way faster than the calculation for general shapes!"

Describe the solution you'd like

I'd like a configurable setting e.g. docstring_inherit_strategy that can be changed to realize a "plain insertion" of parent docstrings for members or to "merge" docstrings from the inheritance tree.

I'm already working on a solution as I saw that contributions are always welcome 😃

Describe alternatives you've considered

Additional context

[^3]: Compare MetaLearner.evaluate with e.g. RLearner.evaluate. Just before Parameters, RLearner has an additional paragraph that stems from this docstring

thomasmarwitz commented 1 month ago

Closing as this effort is now continued here: https://github.com/mkdocstrings/griffe-inherited-docstrings/issues/2