japreiss
japreiss

Reputation: 11251

Sphinx autodoc: inherit docstrings without inheriting class

I have two classes which share an API through duck typing, but do not share any inheritance:

class A:
    def method():
        """Does something."""
        pass

class B:
    def method():
        pass

I have written docstrings for A, but not B. Since the two classes share the same API semantics, their docstrings are mostly the same. I do not wish to repeat the same docstrings in two different places.

If B inherited from A, then autodoc has tools to inherit parent class docstrings, but I don't want B to inherit from A.

I could make both classes inherit from an abc.ABC abstract class and put the docstrings there, but that would be a lot of added code for no purpose other than documentation.

Is there any way to tell autodoc to copy the docstrings from another class?

Upvotes: 0

Views: 648

Answers (1)

Blair
Blair

Reputation: 15818

I'm not sure you can tell autodoc to do that, but you can copy it in the code:

class A:
    def method():
        """Does something."""
        pass

class B:
    def method():
        pass
    method.__doc__ = A.method.__doc__

Upvotes: 2

Related Questions