Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator to inherit docstring #9

Closed
haiiliin opened this issue Oct 27, 2023 · 0 comments
Closed

Decorator to inherit docstring #9

haiiliin opened this issue Oct 27, 2023 · 0 comments

Comments

@haiiliin
Copy link

haiiliin commented Oct 27, 2023

Using a decorator to inherit the docstring would be very useful. For example, the decorator's definition would be like this:

from typing import Callable, Literal

def inherit_docstring(func: Callable = None, *, source: Callable = None, style: Literal["numpy", "google"] = "numpy"):
    """Decorator for inherit docstring from other functions."""
    from docstring_inheritance import inherit_google_docstring, inherit_numpy_docstring

    assert source is not None, "Source function is not given."
    assert style in ("numpy", "google"), "Style must be either 'numpy' or 'google'."

    def wrapper(f):
        inherit_func = inherit_numpy_docstring if style == "numpy" else inherit_google_docstring
        inherit_func(source.__doc__, f)
        return f

    return wrapper if func is None else wrapper(func)

With the following usage:

def parent(x, y):
  """Parent summary.

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

  Notes:
      Parent notes.
  """

@inherit_docstring(source=parent, style="google")
def child(x, y, z):
  """
  Args:
      z: Description for z.

  Returns:
      Something.

  Notes:
      Child notes.
  """

The child's docstring is:

>>> print(child.__doc__)
Parent summary.

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

Returns:
    Something.

Notes:
    Child notes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant