Skip to content

Commit

Permalink
fix: inheriting Args no filtering when child docs is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine DECHAUME committed Jun 5, 2023
1 parent f5f12c0 commit 0410f8d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/docstring_inheritance/docstring_inheritors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,16 @@ def _inherit_sections(
cast(Dict[str, str], child_sections[section_name])
)

if section_name in cls._ARGS_SECTION_ITEMS_NAMES:
temp_section_items = cls._inherit_section_items_with_args(
child_func,
temp_section_items,
)

temp_sections[section_name] = temp_section_items

# Order the standard sections.
# Args section shall be filtered.
for section_name in temp_sections.keys() & cls._ARGS_SECTION_NAMES:
temp_sections[section_name] = cls._filter_args_section(
child_func,
cast(Dict[str, str], temp_sections[section_name]),
)

# Reorder the standard sections.
new_child_sections = {
section_name: temp_sections.pop(section_name)
for section_name in cls._SECTION_NAMES
Expand Down
97 changes: 97 additions & 0 deletions tests/test_base_inheritor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,103 @@ def func_all(arg1, arg2=None, *varargs, **varkw):
pass


@pytest.fixture(scope="module")
def concrete_inheritor() -> type[AbstractDocstringInheritor]:
"""Return a concrete enough AbstractDocstringInheritor."""
AbstractDocstringInheritor._ARGS_SECTION_NAMES = {"Args"}
AbstractDocstringInheritor._SECTION_NAMES_WITH_ITEMS = {"Args", "Methods"}
yield AbstractDocstringInheritor
delattr(AbstractDocstringInheritor, "_ARGS_SECTION_NAMES")
delattr(AbstractDocstringInheritor, "_SECTION_NAMES_WITH_ITEMS")


@pytest.mark.parametrize(
"parent_section,child_section,func,expected",
[
({}, {}, func_none, {}),
# Non-existing section in child.
({"Section": "parent"}, {}, func_none, {"Section": "parent"}),
# Non-existing section in parent.
({}, {"Section": "child"}, func_none, {"Section": "child"}),
# Child section updates the parent one (no items).
({"Section": "parent"}, {"Section": "child"}, func_none, {"Section": "child"}),
# Child section updates the parent one (no items), with other sections.
(
{"Section": "parent", "ParentSection": "parent"},
{"Section": "child", "ChildSection": "child"},
func_none,
{"Section": "child", "ParentSection": "parent", "ChildSection": "child"},
),
# Section reordering.
(
{"Section": "parent", "Returns": "", "Parameters": ""},
{},
func_none,
{"Parameters": "", "Returns": "", "Section": "parent"},
),
# Sections with items (not Args).
# Non-existing item in child.
({"Methods": {"parent_m": ""}}, {}, func_none, {"Methods": {"parent_m": ""}}),
# Non-existing item in parent.
({}, {"Methods": {"child_m": ""}}, func_none, {"Methods": {"child_m": ""}}),
# Child item updates the parent one (no common items).
(
{"Methods": {"parent_m": ""}},
{"Methods": {"child_m": ""}},
func_none,
{"Methods": {"parent_m": "", "child_m": ""}},
),
# Child item updates the parent one (common items).
(
{"Methods": {"method": "parent"}},
{"Methods": {"method": "child"}},
func_none,
{"Methods": {"method": "child"}},
),
# Sections with args items.
# Non-existing section in child for function without args.
({"Args": {"parent_a": ""}}, {}, func_none, {"Args": {}}),
# Non-existing section in parent for function without args.
({}, {"Args": {"child_a": ""}}, func_none, {"Args": {}}),
# Missing argument description.
(
{"Args": {"parent_a": ""}},
{"Args": {"child_a": ""}},
func_args,
{"Args": {"arg": "The description is missing."}},
),
# Argument description in parent.
(
{"Args": {"arg": "parent"}},
{"Args": {"child_a": ""}},
func_args,
{"Args": {"arg": "parent"}},
),
# Argument description in child.
(
{"Args": {"parent_a": ""}},
{"Args": {"arg": "child"}},
func_args,
{"Args": {"arg": "child"}},
),
# Argument description in both parent and child.
(
{"Args": {"arg": "parent"}},
{"Args": {"arg": "child"}},
func_args,
{"Args": {"arg": "child"}},
),
],
)
def test_inherit_items(
concrete_inheritor, parent_section, child_section, func, expected
):
assert (
concrete_inheritor._inherit_sections(parent_section, child_section, func)
== expected
)


@pytest.mark.parametrize(
"func,section_items,expected",
[
Expand Down

0 comments on commit 0410f8d

Please sign in to comment.