Coverage for tests/test_extension.py: 100.00%
22 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-05 17:16 +0200
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-05 17:16 +0200
1"""Tests for the extension."""
3from __future__ import annotations
5from griffe import Extensions, temporary_visited_package
7from griffe_inherited_docstrings import InheritDocstringsExtension
10def test_inherit_docstrings() -> None:
11 """Inherit docstrings from parent classes."""
12 with temporary_visited_package(
13 "package",
14 modules={
15 "__init__.py": """
16 class Parent:
17 def method(self):
18 '''Docstring from parent method.'''
19 class Child(Parent):
20 def method(self):
21 ...
22 """,
23 },
24 extensions=Extensions(InheritDocstringsExtension()),
25 ) as package:
26 assert package["Child.method"].docstring.value == package["Parent.method"].docstring.value
29def test_inherit_and_merge_docstrings() -> None:
30 """Inherit and merge docstrings from parent classes."""
31 attr_doc = "Attribute docstring from class"
32 meth_doc = "Method docstring from class"
33 code = f"""
34 # Base docstrings.
35 class A:
36 attr = 42
37 '''{attr_doc} A.'''
39 def meth(self):
40 '''{meth_doc} A.'''
43 # Redeclare members but without docstrings.
44 class B(A):
45 attr = 42
47 def meth(self):
48 ...
51 # Redeclare members but with empty docstrings.
52 class C(B):
53 attr = 42
54 ''''''
56 def meth(self):
57 ''''''
60 # Redeclare members with docstrings.
61 class D(C):
62 attr = 42
63 '''{attr_doc} D.'''
65 def meth(self):
66 '''{meth_doc} D.'''
69 # Redeclare members with docstrings again.
70 class E(D):
71 attr = 42
72 '''{attr_doc} E.'''
74 def meth(self):
75 '''{meth_doc} E.'''
76 """
77 with temporary_visited_package(
78 "package",
79 modules={"__init__.py": code},
80 extensions=Extensions(InheritDocstringsExtension(merge=True)),
81 ) as package:
82 assert package["B.attr"].docstring.value == package["A.attr"].docstring.value
83 assert package["B.meth"].docstring.value == package["A.meth"].docstring.value
84 assert package["C.attr"].docstring.value == package["A.attr"].docstring.value
85 assert package["C.meth"].docstring.value == package["A.meth"].docstring.value
86 assert package["D.attr"].docstring.value == package["A.attr"].docstring.value + "\n\n" + f"{attr_doc} D."
87 assert package["D.meth"].docstring.value == package["A.meth"].docstring.value + "\n\n" + f"{meth_doc} D."
88 assert package["E.attr"].docstring.value == package["D.attr"].docstring.value + "\n\n" + f"{attr_doc} E."
89 assert package["E.meth"].docstring.value == package["D.meth"].docstring.value + "\n\n" + f"{meth_doc} E."
92def test_inherit_and_merge_docstrings_intermediate_class() -> None:
93 """Inherit and merge docstrings from parent classes with an intermediate class.
95 It is important that the intermediate class doesn't have the member for which
96 docstring inheritance should be performed.
97 """
98 with temporary_visited_package(
99 "package",
100 modules={
101 "__init__.py": """
102 class Parent:
103 def method(self):
104 '''Parent.'''
106 class Intermediate(Parent):
107 # This shouldn't break the inherting of docstrings.
108 # See https://github.com/mkdocstrings/griffe-inherited-docstrings/issues/4.
109 ...
111 class Child(Intermediate):
112 def method(self):
113 '''Child.'''
114 """,
115 },
116 extensions=Extensions(InheritDocstringsExtension(merge=True)),
117 ) as package:
118 assert package["Child.method"].docstring.value == "Parent.\n\nChild."