Skip to content

Commit

Permalink
refactor: Add separate test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmarwitz committed Nov 5, 2024
1 parent ff5c2f7 commit a8e0884
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/griffe_inherited_docstrings/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def _docstring_above(obj: Object) -> Docstring | None:
with contextlib.suppress(IndexError, KeyError):
for parent in obj.parent.mro(): # type: ignore[union-attr]
for parent in obj.parent.mro(): # type: ignore[union-attr]
# Fetch docstring from first parent that has the member
if obj.name in parent.members:
return parent.members[obj.name].docstring
Expand Down
32 changes: 26 additions & 6 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ class A:
def meth(self):
'''{meth_doc} A.'''
class Intermediate(A):
# This shouldn't break the inherting of docstrings.
# See https://github.com/mkdocstrings/griffe-inherited-docstrings/issues/4
pass
# Redeclare members but without docstrings.
class B(Intermediate):
class B(A):
attr = 42
def meth(self):
Expand Down Expand Up @@ -91,3 +86,28 @@ def meth(self):
assert package["D.meth"].docstring.value == package["A.meth"].docstring.value + "\n\n" + f"{meth_doc} D."
assert package["E.attr"].docstring.value == package["D.attr"].docstring.value + "\n\n" + f"{attr_doc} E."
assert package["E.meth"].docstring.value == package["D.meth"].docstring.value + "\n\n" + f"{meth_doc} E."


def test_inherit_and_merge_docstrings_intermediate_class() -> None:
"""Inherit docstrings from parent classes."""
with temporary_visited_package(
"package",
modules={
"__init__.py": """
class Parent:
def method(self):
'''Parent.'''
class Intermediate(Parent):
# This shouldn't break the inherting of docstrings.
# See https://github.com/mkdocstrings/griffe-inherited-docstrings/issues/4
...
class Child(Parent):
def method(self):
'''Child.'''
""",
},
extensions=Extensions(InheritDocstringsExtension(merge=True)),
) as package:
assert package["Child.method"].docstring.value == "Parent." + "\n\n" + "Child."

0 comments on commit a8e0884

Please sign in to comment.