docrep: Docstring Repetition

0.3.2 · active · verified Thu Apr 16

docrep is a Python library designed for intelligent reuse of docstrings, following the 'Don't Repeat Yourself' (DRY) principle. It helps developers manage documentation in complex and nested Python APIs by providing tools to analyze, update, and repeat sections of docstrings, particularly those adhering to NumPy conventions. The latest stable version is 0.3.2, released in February 2021, indicating a slower release cadence.

Common errors

Warnings

Install

Imports

Quickstart

Initialise `DocstringProcessor` and use its decorators to extract and reuse sections of docstrings, for example, the parameters and returns from a base function's NumPy-style docstring.

from docrep import DocstringProcessor

docstrings = DocstringProcessor()

@docstrings.get_sections
def my_base_function(param_a: int, param_b: str):
    """
    This is a base function.

    Parameters
    ----------
    param_a : int
        The first parameter.
    param_b : str
        The second parameter.

    Returns
    -------
    bool
        True if the operation was successful.
    """
    print(f"Base function called with {param_a}, {param_b}")
    return True

@docstrings.dedent
def my_derived_function(param_a: int, param_b: str, param_c: float):
    """
    This is a derived function that reuses docstrings.

    Parameters
    ----------
    %(my_base_function.parameters)s
    param_c : float
        A third, specific parameter.

    Returns
    -------
    %(my_base_function.returns)s
    """
    print(f"Derived function called with {param_a}, {param_b}, {param_c}")
    return my_base_function(param_a, param_b)

if __name__ == "__main__":
    print("--- Docstring for my_derived_function ---")
    print(my_derived_function.__doc__)
    print("\n--- Calling my_derived_function ---")
    result = my_derived_function(1, "hello", 3.14)
    print(f"Result: {result}")

view raw JSON →