-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""The Griffe extension.""" | ||
|
||
from __future__ import annotations | ||
|
||
import contextlib | ||
from typing import TYPE_CHECKING | ||
|
||
from griffe import Extension | ||
from griffe.exceptions import AliasResolutionError | ||
|
||
if TYPE_CHECKING: | ||
from griffe import Docstring, Module, Object | ||
|
||
|
||
def _inherited_docstring(obj: Object) -> Docstring | None: | ||
for parent_class in obj.parent.mro(): # type: ignore[union-attr] | ||
try: | ||
if docstring := parent_class.members[obj.name].docstring: | ||
return docstring | ||
except KeyError: | ||
pass | ||
return None | ||
|
||
|
||
def _inherit_docstrings(obj: Object) -> None: | ||
if obj.is_module: | ||
for member in obj.members.values(): | ||
if not member.is_alias: | ||
with contextlib.suppress(AliasResolutionError): | ||
_inherit_docstrings(member) # type: ignore[arg-type] | ||
elif obj.is_class: | ||
for member in obj.members.values(): | ||
if not member.is_alias: | ||
if member.docstring is None and (inherited := _inherited_docstring(member)): # type: ignore[arg-type] | ||
member.docstring = inherited | ||
if member.is_class: | ||
_inherit_docstrings(member) # type: ignore[arg-type] | ||
|
||
|
||
class InheritDocstringsExtension(Extension): | ||
"""Griffe extension for inheriting docstrings.""" | ||
|
||
def on_package_loaded(self, *, pkg: Module) -> None: | ||
"""Inherit docstrings from parent classes once the whole package is loaded.""" | ||
_inherit_docstrings(pkg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Tests for the extension.""" | ||
|
||
from __future__ import annotations | ||
|
||
from griffe.extensions import Extensions | ||
from griffe.tests import temporary_visited_package | ||
|
||
from griffe_inherited_docstrings import InheritDocstringsExtension | ||
|
||
|
||
def test_inherit_docstrings() -> None: | ||
"""Inherit docstrings from parent classes.""" | ||
with temporary_visited_package( | ||
"package", | ||
modules={ | ||
"__init__.py": """ | ||
class Parent: | ||
def method(self): | ||
'''Docstring from parent method.''' | ||
class Child(Parent): | ||
def method(self): | ||
... | ||
""", | ||
}, | ||
extensions=Extensions(InheritDocstringsExtension()), | ||
) as package: | ||
assert package["Child.method"].docstring.value == package["Parent.method"].docstring.value |