custom_inherit

Build Status PyPi version Conda Version Python version support

Contents

Overview

The Python package custom_inherit provides convenient, light-weight tools for inheriting docstrings in customizeable ways.

Features

Implementation Notes

Projects That Use custom_inherit

Basic Usage

Inheriting Docstrings Using a Metaclass

custom_inherit exposes a metaclass, DocInheritMeta(), that, when derived from by a class, will automatically mediate docstring inheritance for all subsequent derived classes of that parent. Thus a child's attributes (methods, classmethods, staticmethods, properties, and their abstract counterparts) will inherit documentation from its parent's attribute, and the resulting docstring is synthesized according to a customizable style.

The style of the inheritance scheme can be specified explicitly when passing DocInheritMeta its arguments. Here is a simple usage example using the built-in "numpy" style of inheritance:

from custom_inherit import DocInheritMeta

class Parent(metaclass=DocInheritMeta(style="numpy")):
   def meth(self, x, y=None):
       """ Parameters
           ----------
           x: int
              blah-x

           y: Optional[int]
              blah-y

           Raises
           ------
           NotImplementedError"""
       raise NotImplementedError

class Child(Parent):
    def meth(self, x, y=None):
        """ Method description

            Returns
            -------
            int

            Notes
            -----
            Some notes here."""
        return 0

Because we specified style="numpy" in DocInheritMeta, the inherited docstring of Child.meth will be:

  """ Method description

      Parameters
      ----------
      x: int
         blah-x

      y: Optional[int]
         blah-y

      Returns
      -------
      int

      Notes
      -----
      Some notes here."""

(note the special case where the "Raises" section of the parent's method is left out, because the child class implements a "Returns" section instead. Jump ahead for a detailed description of the "numpy" style)

Keep in mind that the syntax for deriving from a meta class is slightly different in Python 2:

from custom_inherit import DocInheritMeta

class Parent(object):
   __metaclass__ = DocInheritMeta(style="numpy")
   ...

Inheriting Docstrings Using a Decorator

custom_inherit also exposes a decorator capable of mediating docstring inheritance on an individual function (or property, method, etc.) level. In this example, we provide our own custom inheritance style-function on the fly (rather than using a built-in style):

from custom_inherit import doc_inherit

def my_style(prnt_doc, child_doc): return "\n-----".join([prnt_doc, child_doc])

def parent():  # parent can be any object with a docstring, or simply a string itself
   """ docstring to inherit from"""

@doc_inherit(parent, style=my_style)
def child():
   """ docstring to inherit into"""

Given the customized (albeit stupid) inheritance style specified in this example, the inherited docsting of child, in this instance, will be:

"""docstring to inherit from
  -----
  docstring to inherit into"""

Advanced Usage

A very natural, but more advanced use case for docstring inheritance is to define an abstract base class that has detailed docstrings for its abstract methods/properties. This class can be passed DocInheritMeta(abstract_base_class=True), and it will have inherited from abc.ABCMeta, plus all of its derived classes will inherit the docstrings for the methods/properties that they implement:

# Parent is now an abstract base class
class Parent(metaclass=DocInheritMeta(style="numpy", abstract_base_class=True)):
   ...

For the "numpy", "google", and "napoleon_numpy" inheritance styles, one then only needs to specify the "Returns" or "Yields" section in the derived class' attribute docstring for it to have a fully-detailed docstring.

Another option is to be able to decide whether to include all special methods, meaning methods that start and end by "" such as "init__" method, or not in the doctstring inheritance process. Such an option can be pass to the DocInheritMeta metaclass constructor:

# Each child class will also merge the special methods' docstring of its parent class
class Parent(metaclass=DocInheritMeta(style="numpy", include_special_methods=True)):
   ...

Special methods are not included by default.

Built-in Styles

Utilize a built-in style by specifying any of the following names (as a string), wherever the style parameter is to be specified. The built-in styles are:

For the numpy, numpy_with_merge, numpy_napoleon, numpy_napoleon_with_merge, google and google_with_merge styles, if the parent's docstring contains a "Raises" section and the child's docstring implements a "Returns" or a "Yields" section instead, then the "Raises" section is not included in the resulting docstring. This is to accomodate for the relatively common use case in which an abstract method/property raises NotImplementedError. Child classes that implement this method/property clearly will not raise this. Of course, any "Raises" section that is explicitly included in the child's docstring will appear in the resulting docstring.

Detailed documentation and example cases for the default styles can be found here

Making New Inheritance Styles

Implementing your inheritance style is simple.

Installation and Getting Started

Install via pip:

    pip install custom_inherit

Install via conda:

    conda install -c conda-forge custom-inherit

or

Download/clone this repository, go to its directory, and install custom_inherit by typing in your command line:

    python setup.py install

If, instead, you want to install the package with links, so that edits you make to the code take effect immediately within your installed version of custom_inherit, type:

    python setup.py develop

and then get started with

from custom_inherit import DocInheritMeta, doc_inherit, store
# print(store) shows you the available styles

Documentation

Documentation is available via help(custom_inherit).

custom_inherit.DocInheritMeta(style="parent", abstract_base_class=False):
    """ A metaclass that merges the respective docstrings of a parent class and of its child, along with their
        properties, methods (including classmethod, staticmethod, decorated methods).

        Parameters
        ----------
        style: Union[Hashable, Callable[[str, str], str]], optional (default: "parent")
            A valid inheritance-scheme style ID or function that merges two docstrings.

        abstract_base_class: bool, optional(default: False)
            If True, the returned metaclass inherits from abc.ABCMeta.

            Thus a class that derives from DocInheritMeta(style="numpy", abstract_base_class=True)
            will be an abstract base class, whose derived classes will inherit docstrings
            using the numpy-style inheritance scheme.

        Returns
        -------
        custom_inherit.DocInheritorBase"""

custom_inherit.doc_inherit(parent, style="parent"):
    """ Returns a function/method decorator that, given `parent`, updates the docstring of the decorated
        function/method based on the specified style and the corresponding attribute of `parent`.

        Parameters
        ----------
        parent : Union[str, Any]
            The object whose docstring, is utilized as the parent docstring
        during the docstring merge. Or, a string can be provided directly.

        style : Union[Hashable, Callable[[str, str], str]], optional (default: "parent")
            A valid inheritance-scheme style ID or function that merges two docstrings.

        Returns
        -------
        custom_inherit.DocInheritDecorator

        Notes
        -----
        `doc_inherit` should always be the inner-most decorator when being used in
        conjunction with other decorators, such as `@property`, `@staticmethod`, etc."""

custom_inherit.remove_style(style):
    """ Remove the specified style from the style store.

        Parameters
        ----------
        style: Hashable
            The inheritance-scheme style ID to be removed."""

custom_inherit.add_style(style_name, style_func):
    """ Make available a new function for merging a 'parent' and 'child' docstring.

        Parameters
        ----------
        style_name : Hashable
            The identifier of the style being logged
        style_func: Callable[[Optional[str], Optional[str]], Optional[str]]
            The style function that merges two docstrings into a single docstring."""

Go Back To: