Skip to content

Commit

Permalink
Add inherit_docstring decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
haiiliin committed Oct 27, 2023
1 parent be17920 commit daddf96
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ Notes:
"""
```

## Using decorators

Functions and methods can be decorated with the `inherit_docstring` decorator
to inherit the docstring of another function or method. For example, the above
example can be rewritten as:

```python
from docstring_inheritance import inherit_docstring


def parent():
"""Parent summary.
Args:
x: Description for x.
y: Description for y.
Notes:
Parent notes.
"""


@inherit_docstring(source=parent, style="google")
def child():
"""
Args:
z: Description for z.
Returns:
Something.
Notes:
Child notes.
"""
```

# Docstring inheritance specification

## Sections order
Expand Down
15 changes: 14 additions & 1 deletion src/docstring_inheritance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# SOFTWARE.
from __future__ import annotations

from typing import Any
from typing import Any, Callable, Literal

from .class_docstrings_inheritor import ClassDocstringsInheritor
from .class_docstrings_inheritor import DocstringInheritor
Expand Down Expand Up @@ -120,3 +120,16 @@ def __init__(
inherit_numpy_docstring,
init_in_class=True,
)


def inherit_docstring(func: Callable = None, *, source: Callable = None, style: Literal["numpy", "google"] = "numpy"):
"""Decorator for inherit docstring from other functions."""
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)
33 changes: 33 additions & 0 deletions tests/test_inheritance_for_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import pytest

from docstring_inheritance import inherit_docstring
from docstring_inheritance import inherit_google_docstring
from docstring_inheritance import inherit_numpy_docstring
from docstring_inheritance.class_docstrings_inheritor import ClassDocstringsInheritor
Expand Down Expand Up @@ -131,6 +132,38 @@ def child(x, missing_doc, *child_varargs, **child_kwargs):
assert child.__doc__ == expected.strip("\n")


def test_decorator():
def parent(arg, *parent_varargs, **parent_kwargs):
"""Parent summary.
Args:
arg: desc
*parent_varargs: Parent *args
**parent_kwargs: Parent **kwargs
"""

@inherit_docstring(source=parent, style="google")
def child(x, missing_doc, *child_varargs, **child_kwargs):
"""Child summary.
Args:
x: X
child_varargs: Not *args
*child_varargs: Child *args
**child_kwargs: Child **kwargs
"""

expected = """Child summary.
Args:
x: X
missing_doc: The description is missing.
*child_varargs: Child *args
**child_kwargs: Child **kwargs
"""
assert child.__doc__ == expected.strip("\n")


@pytest.mark.parametrize(
"inherit_docstring", [inherit_numpy_docstring, inherit_google_docstring]
)
Expand Down

0 comments on commit daddf96

Please sign in to comment.