Skip to main content

A python decorator to inherit docstring.

Project description

inherit-docstring

test test coverage

A python decorator to inherit docstring.

Easily inherit docstrings from parent classes with @inherit_docstring decorator, designed specifically for the NumPy docstring style.

Use inherit-docstring to streamline your documentation process, ensuring consistency and reducing redundancy!

Features

  • Automatic Inheritance: Just add the @inherit_docstring decorator, and the child class will seamlessly inherit the parent's class and function docstrings.
  • Structured Sections: Docstrings are broken into sections like Attributes, Notes, etc. Each section is denoted by its title followed by ---.
  • Header Section: An exclusive Header section is introduced for the starting portion of the docstring without a specific title.
  • Parameter Sections: Certain sections are treated as parameter sections where the content is interpreted as parameter explanations. They include:
    • Attributes
    • Parameters
    • Returns
    • Yields
    • Receives
    • Raises
    • Warns
    • Warnings
  • Deprecated sections: Sections starting with .. deprecated:: x.y.z, is parsed as deprecated sections.

Behavior

  • If a child class function lacks a docstring, it inherits the parent's docstring verbatim.
  • For functions where both parent and child have docstrings:
    • Section-wise Merge: Docstrings are combined on a section-by-section basis.
    • Parameter-wise Merge: Within parameter sections, docstrings are combined parameter by parameter.
    • Child Priority: When both parent and child provide docstrings for the same function or parameter, the child's version is prioritized.

Requirement

  • Python >=3.9
  • Poetry (For development)

Installation

By pip:

$ pip3 install inherit-docstring

Usage

Add inherit_docstring decorator to the inherited class:

from inherit_docstring import inherit_docstring

class Parent:
    """Parent class.

    This is an explanation.

    Attributes
    ----------
    name: str
        The name of
        the parent.
    age:
        The age. w/o type.

    Notes
    -----
    This is parent's note.
    """

    name: str = 'parent'
    age: int = 40

    def func1(self, param1: int, param2: int) -> int:
        """Parent's func1.

        Parameters
        ----------
        param1: int
            First input.
        param2: int
            Second input.

        Returns
        -------
        ret: int
            param1 + param2
        """

        return param1 + param2

    def func2(self) -> None:
        """Parent's func2.

        Returns
        -------
        ret: str
            something
        """

        return 'Something'

@inherit_docstring
class Child(Parent):
    """Child class.

    Attributes
    ----------
    sex: str
        Additional attributes.
        girl or boy.
    """

    sex: str = "boy"

    def func1(self, param1: int, param2: int) -> int:
        """Child's func1.

        Returns
        -------
        ret: int
            param1 - param2
        """

        return param1 - param2

Child class' help will be:

class Child(Parent)
 |  Child class.
 |
 |  Attributes
 |  ----------
 |  name: str
 |      The name of
 |      the parent.
 |  age:
 |      The age. w/o type.
 |  sex: str
 |      Additional attributes.
 |      girl or boy.
 |
 |  Notes
 |  -----
 |  This is parent's note.
 |
 |  Method resolution order:
 |      Child
 |      Parent
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  func1(self, param1: int, param2: int) -> int
 |      Child's func1.
 |
 |      Parameters
 |      ----------
 |      param1: int
 |          First input.
 |      param2: int
 |          Second input.
 |
 |      Returns
 |      -------
 |      ret: int
 |          param1 - param2

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

inherit_docstring-0.1.4.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

inherit_docstring-0.1.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file inherit_docstring-0.1.4.tar.gz.

File metadata

  • Download URL: inherit_docstring-0.1.4.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.4.0

File hashes

Hashes for inherit_docstring-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9fca2f69aa9f140ada0a3b67430163bb0e0d6e6a3f28e1a3bf4c4dba1134aa0c
MD5 479e597f8af34b84c9433dd43755a873
BLAKE2b-256 83f82110b342dc41fb6169b952b9673704cf41735c71a0947690dbb9844917f9

See more details on using hashes here.

File details

Details for the file inherit_docstring-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for inherit_docstring-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9e77f7e20e6faf0a924244967abd37380233679d1680e102a2ff1e49fd0c74a1
MD5 a7aef27401d350a76355198eb10d2f35
BLAKE2b-256 b87f7ff1a8cffc40c9cc8d89660cb91841f903a1e0b2bf03a0bd2a5f6e67ee5f

See more details on using hashes here.

Supported by

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