Is there a way to let classes inherit the documentation of their superclass using sphinx?

Suppose I have a class

class A(object):
  def myfunction():
    """A."""
    pass

and subclass

class B(A):
  def myfunction():
    pass

Is it possible to inherit the B.myfunction API document from A.myfunction with the sphinx? The documentation for B.myfunction should be "A." also.

+3
source share
1 answer

python , "docstring" __doc__ . - , docstring . docstring B.myfunction A.myfunction ( , -, ). ( ), docstring :

def copydoc(fromfunc, sep="\n"):
    """
    Decorator: Copy the docstring of `fromfunc`
    """
    def _decorator(func):
        sourcedoc = fromfunc.__doc__
        if func.__doc__ == None:
            func.__doc__ = sourcedoc
        else:
            func.__doc__ = sep.join([sourcedoc, func.__doc__])
        return func
    return _decorator

class A(object):
  def myfunction():
    """Documentation for A."""
    pass

class B(A):
  @copydoc(A.myfunction)
  def myfunction():
    """Extra details for B."""
    pass

:

>>> help(B.myfunction)
Help on method myfunction in module __main__:

myfunction() unbound __main__.B method
    Documentation for A.
    Extra details for B.

, docstring : @copydoc(A.myfunction). , , , , , .

, , : " ", , . , , @copydoc(A). , . ( , , ).

+4

Source: https://habr.com/ru/post/1660301/


All Articles