From a8e088485a1d541d4a6192175ae3d25cae3492a3 Mon Sep 17 00:00:00 2001 From: thomasmarwitz Date: Tue, 5 Nov 2024 14:15:21 +0100 Subject: [PATCH] refactor: Add separate test --- src/griffe_inherited_docstrings/extension.py | 2 +- tests/test_extension.py | 32 ++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/griffe_inherited_docstrings/extension.py b/src/griffe_inherited_docstrings/extension.py index 695b8c0..9ff2cf7 100644 --- a/src/griffe_inherited_docstrings/extension.py +++ b/src/griffe_inherited_docstrings/extension.py @@ -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 diff --git a/tests/test_extension.py b/tests/test_extension.py index 08476b2..995f9c4 100644 --- a/tests/test_extension.py +++ b/tests/test_extension.py @@ -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): @@ -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."