Skip to main content

Avoid writing and maintaining duplicated docstrings.

Project description

PyPI - Python Version PyPI Conda (channel only) Codecov branch

docstring-inheritance is a python package to avoid writing and maintaining duplicated python docstrings. The typical usage is to enable the inheritance of the docstrings from a base class such that its derived classes fully or partly inherit the docstrings.

Features

  • Handle numpy and google docstring formats (i.e. sections based docstrings):
  • Handle docstrings for functions, classes, methods, class methods, static methods, properties.
  • Handle docstrings for classes with multiple or multi-level inheritance.
  • Docstring sections are inherited individually, like methods.
  • For docstring sections documenting signatures, the signature arguments are inherited individually.
  • Minimum performance cost: the inheritance is performed at import time, not for each call.
  • Compatible with rendering the documentation with Sphinx and mkdocs (See below).
  • Missing docstring sections for signature arguments can be notified by warnings when the environment variable DOCSTRING_INHERITANCE_WARNS is set.
  • Docstring sections can be compared to detect duplicated or similar contents that could be inherited.

Licenses

The source code is distributed under the MIT license. The documentation is distributed under the CC BY 4.0 license. The dependencies, with their licenses, are given in the CREDITS.md file.

Installation

Install with pip:

pip install docstring-inheritance

Or with conda:

conda install -c conda-forge docstring-inheritance

Basic Usage

Inheriting docstrings for classes

docstring-inheritance provides metaclasses to enable the docstrings of a class to be inherited from its base classes. This feature is automatically transmitted to its derived classes as well. The docstring inheritance is performed for the docstrings of the:

  • class
  • methods
  • classmethods
  • staticmethods
  • properties

Use the NumpyDocstringInheritanceMeta metaclass to inherit docstrings in numpy format if __init__ method is documented in its own docstring. Otherwise, if __init__ method is documented in the class docstring, use the NumpyDocstringInheritanceInitMeta metaclass.

Use the GoogleDocstringInheritanceMeta metaclass to inherit docstrings in google format. if __init__ method is documented in its own docstring. Otherwise, if __init__ method is documented in the class docstring, use the GoogleDocstringInheritanceInitMeta metaclass.

from docstring_inheritance import NumpyDocstringInheritanceMeta


class Parent(metaclass=NumpyDocstringInheritanceMeta):
  def method(self, x, y=None):
    """Parent summary.

    Parameters
    ----------
    x:
       Description for x.
    y:
       Description for y.

    Notes
    -----
    Parent notes.
    """


class Child(Parent):
  def method(self, x, z):
    """
    Parameters
    ----------
    z:
       Description for z.

    Returns
    -------
    Something.

    Notes
    -----
    Child notes.
    """


# The inherited docstring is
Child.method.__doc__ == """Parent summary.

Parameters
----------
x:
   Description for x.
z:
   Description for z.

Returns
-------
Something.

Notes
-----
Child notes.
"""

Inheriting docstrings for functions

docstring-inheritance provides functions to inherit the docstring of a callable from a string. This is typically used to inherit the docstring of a function from another function.

Use the inherit_google_docstring function to inherit docstrings in google format.

Use the inherit_numpy_docstring function to inherit docstrings in numpy format.

from docstring_inheritance import inherit_google_docstring


def parent():
    """Parent summary.

    Args:
        x: Description for x.
        y: Description for y.

    Notes:
        Parent notes.
    """


def child():
    """
    Args:
        z: Description for z.

    Returns:
        Something.

    Notes:
        Child notes.
    """


inherit_google_docstring(parent.__doc__, child)

# The inherited docstring is
child.__doc__ == """Parent summary.

Args:
    x: Description for x.
    z: Description for z.

Returns:
    Something.

Notes:
    Child notes.
"""

Docstring inheritance specification

Sections order

The sections of an inherited docstring are sorted according to order defined in the NumPy docstring format specification:

  • Summary
  • Extended summary
  • Parameters for the NumPy format or Args for the Google format
  • Returns
  • Yields
  • Receives
  • Other Parameters
  • Attributes
  • Methods
  • Raises
  • Warns
  • Warnings
  • See Also
  • Notes
  • References
  • Examples
  • sections with other names come next

This ordering is also used for the docstring written with the Google docstring format specification even though it does not define all of these sections.

Sections with items

Those sections are:

  • Other Parameters
  • Methods
  • Attributes

The inheritance is done at the key level, i.e. a section of the inheritor will not fully override the parent one:

  • the keys in the parent section and not in the child section are inherited,
  • the keys in the child section and not in the parent section are kept,
  • for keys that are both in the parent and child section, the child ones are kept.

This allows to only document the new keys in such a section of an inheritor. For instance:

from docstring_inheritance import NumpyDocstringInheritanceMeta


class Parent(metaclass=NumpyDocstringInheritanceMeta):
  """
  Attributes
  ----------
  x:
     Description for x
  y:
     Description for y
  """


class Child(Parent):
  """
  Attributes
  ----------
  y:
     Overridden description for y
  z:
     Description for z
  """


# The inherited docstring is
Child.__doc__ == """
Attributes
----------
x:
   Description for x
y:
   Overridden description for y
z:
   Description for z
"""

Here the keys are the attribute names. The description for the attribute y has been overridden and the description for the attribute z has been added. The only remaining description from the parent is for the attribute x.

Sections documenting signatures

Those sections are:

  • Parameters (numpy format only)
  • Args (google format only)

In addition to the inheritance behavior described above:

  • the arguments not existing in the inheritor signature are removed,
  • the arguments are sorted according the inheritor signature,
  • the arguments with no description are provided with a dummy description.
from docstring_inheritance import GoogleDocstringInheritanceMeta


class Parent(metaclass=GoogleDocstringInheritanceMeta):
  def method(self, w, x, y):
    """
    Args:
        w: Description for w
        x: Description for x
        y: Description for y
    """


class Child(Parent):
  def method(self, w, y, z):
    """
    Args:
        z: Description for z
        y: Overridden description for y
    """


# The inherited docstring is
Child.method.__doc__ == """
Args:
    w: Description for w
    y: Overridden description for y
    z: Description for z
"""

Here the keys are the argument names. The description for the argument y has been overridden and the description for the argument z has been added. The only remaining description from the parent is for the argument w.

Advanced usage

Abstract base class

To create a parent class that both is abstract and has docstring inheritance, an additional metaclass is required:

import abc
from docstring_inheritance import NumpyDocstringInheritanceMeta


class Meta(abc.ABCMeta, NumpyDocstringInheritanceMeta):
  pass


class Parent(metaclass=Meta):
  pass

Detecting similar docstrings

Duplicated docstrings that could benefit from inheritance can be detected by setting the environment variable DOCSTRING_INHERITANCE_SIMILARITY_RATIO to a value between 0 and 1. When set, the docstring sections of a child and its parent are compared and warnings are issued when the docstrings are similar. The docstring sections are compared with difflib ratio from the standard library. If the ratio is higher or equal to the value of DOCSTRING_INHERITANCE_SIMILARITY_RATIO, the docstring sections are considered similar. Use a ratio of 1 to detect identical docstring sections. Use a ratio lower than 1 to detect similar docstring sections.

Mkdocs

To render the documentation with mkdocs, the package mkdocstring[python] is required and the package griffe-inherited-docstrings is recommended, finally the following shall be added to mkdocs.yml:

plugins:
- mkdocstrings:
    handlers:
      python:
        options:
          extensions:
            - griffe_inherited_docstrings
            - docstring_inheritance.griffe

Similar projects

custom_inherit: docstring-inherit started as fork of this project before being re-written, we thank its author.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docstring_inheritance-2.2.2.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

docstring_inheritance-2.2.2-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file docstring_inheritance-2.2.2.tar.gz.

File metadata

  • Download URL: docstring_inheritance-2.2.2.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for docstring_inheritance-2.2.2.tar.gz
Algorithm Hash digest
SHA256 224eeaee1aeea6503341f9d39eea075d50760f9f2ad9c73dbe8ae61af785d3c0
MD5 a1a34f1eb8dd5c6e855f4afcf378bec5
BLAKE2b-256 16f4988a96d45e7db09e1c43dbb8ab0ef0e093e82869fb38e30d41647571a024

See more details on using hashes here.

File details

Details for the file docstring_inheritance-2.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for docstring_inheritance-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 090d7a6e098bbea8914839391d56cc38e32d37d471d99a3397ef9d94fa84b5e9
MD5 ea27f2b6720d40ed13774e7cfe286d5e
BLAKE2b-256 dfc4a098fe2006371014eddf84244751352c6160c2dc7d2d033aee732f4b2065

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page