inherit_docs decorator#

Sometimes, it can be a challenge to make sure that the derived class and all its (inherited or overwritten) methods ihnerit the documentation strings, unless explicitly overwritten.

Let us say we have a documented class A:

[1]:
class A:
    """Class A doc-string."""

    def __init__(self, a):
        self.a = a

    def method1(self):
        """Class A method doc-string."""
        return self.a - 1

    def method2(self):
        """Class A method doc-string."""
        return self.a + 3

Now, if we derive class B, by decorating it with ubermagutil.inherit_docs we can make sure all doc-strings are ihnerited, unless overwritten explicitly. This is also the case for methods, whose implementation is changed, but doc-string was not specified.

[2]:
import ubermagutil


@ubermagutil.inherit_docs
class B(A):
    """Class B doc-string."""

    def method1(self):
        return self.a + 1

    def method2(self):
        """New doc-string."""
        return self.a + 9

From blass B, method1 inherits doc-string:

[3]:
B.method1.__doc__
[3]:
'Class A method doc-string.'

However, doc-string of method2 is overwritten:

[4]:
B.method2.__doc__
[4]:
'New doc-string.'