0

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?

japreiss
  • 11,111
  • 2
  • 40
  • 77

1 Answers1

2

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__
Blair
  • 15,356
  • 7
  • 46
  • 56