Skip to content

Commit f371bbb

Browse files
committed
_check_toc_parents considers only the descendants of root_doc and not the whole toctree_includes graph
1 parent ed8045c commit f371bbb

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

sphinx/environment/__init__.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def check_consistency(self) -> None:
771771
# Call _check_toc_parents here rather than in _get_toctree_ancestors()
772772
# because that method is called multiple times per document and would
773773
# lead to duplicate warnings.
774-
_check_toc_parents(self.toctree_includes)
774+
_check_toc_parents(self.toctree_includes, self.config.root_doc)
775775

776776
# call check-consistency for all extensions
777777
self.domains._check_consistency()
@@ -819,19 +819,28 @@ def _traverse_toctree(
819819
traversed.add(sub_docname)
820820

821821

822-
def _check_toc_parents(toctree_includes: dict[str, list[str]]) -> None:
822+
def _check_toc_parents(toctree_includes: dict[str, list[str]], root_doc: str) -> None:
823+
"""Checks if document is referenced in multiple toctrees.
824+
Based on the current implementation of `global_toctree_for_doc`,
825+
it considers only the descendants of root_doc and not the whole graph.
826+
"""
823827
toc_parents: dict[str, list[str]] = {}
824-
for parent, children in toctree_includes.items():
825-
for child in children:
826-
toc_parents.setdefault(child, []).append(parent)
827828

829+
def _find_toc_parents_dfs(node: str) -> None:
830+
for child in toctree_includes.get(node, []):
831+
toc_parents.setdefault(child, []).append(node)
832+
is_child_already_visited = len(toc_parents[child]) > 1
833+
if not is_child_already_visited:
834+
_find_toc_parents_dfs(child)
835+
836+
_find_toc_parents_dfs(root_doc)
828837
for doc, parents in sorted(toc_parents.items()):
829838
if len(parents) > 1:
830839
logger.info(
831840
__(
832841
'document is referenced in multiple toctrees: %s, selecting: %s <- %s'
833842
),
834-
parents,
843+
sorted(parents),
835844
max(parents),
836845
doc,
837846
location=doc,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test-toctree-multiple-parents
2+
=============================
3+
4+
.. literalinclude:: relation_graph.txt
5+
6+
.. toctree::
7+
delta

tests/roots/test-toctree-multiple-parents/relation_graph.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
index
2-
/ \
1+
index pdfindex
2+
/ \ /
33
alpha delta
44
\ /
55
bravo /

tests/test_builders/test_build.py

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def test_multiple_parents_toctree(app):
106106
assert (
107107
"document is referenced in multiple toctrees: ['bravo', 'delta'], selecting: delta <- charlie"
108108
) in app.status.getvalue()
109+
assert (
110+
"document is referenced in multiple toctrees: ['index', 'pdfindex'], selecting: pdfindex <- delta"
111+
) not in app.status.getvalue()
109112

110113

111114
@pytest.mark.usefixtures('_http_teapot')

0 commit comments

Comments
 (0)