Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_check_toc_parents should consider only the descendants of root_doc and n… #13038

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ def check_consistency(self) -> None:
# Call _check_toc_parents here rather than in _get_toctree_ancestors()
# because that method is called multiple times per document and would
# lead to duplicate warnings.
_check_toc_parents(self.toctree_includes)
_check_toc_parents(self.toctree_includes, self.config.root_doc)

# call check-consistency for all extensions
self.domains._check_consistency()
Expand Down Expand Up @@ -819,12 +819,22 @@ def _traverse_toctree(
traversed.add(sub_docname)


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

def _find_toc_parents_dfs(node: str) -> None:
for child in toctree_includes.get(node, []):
already_visited = child in toc_parents
toc_parents.setdefault(child, []).append(node)
if already_visited:
continue
_find_toc_parents_dfs(child)

_find_toc_parents_dfs(root_doc)
for doc, parents in sorted(toc_parents.items()):
if len(parents) > 1:
logger.info(
Expand Down
7 changes: 7 additions & 0 deletions tests/roots/test-toctree-multiple-parents/pdfindex.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test-toctree-multiple-parents
=============================

.. literalinclude:: relation_graph.txt

.. toctree::
delta
4 changes: 2 additions & 2 deletions tests/roots/test-toctree-multiple-parents/relation_graph.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
index
/ \
index pdfindex
/ \ /
alpha delta
\ /
bravo /
Expand Down
3 changes: 3 additions & 0 deletions tests/test_builders/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def test_multiple_parents_toctree(app):
assert (
"document is referenced in multiple toctrees: ['bravo', 'delta'], selecting: delta <- charlie"
) in app.status.getvalue()
assert (
"document is referenced in multiple toctrees: ['index', 'pdfindex'], selecting: pdfindex <- delta"
) not in app.status.getvalue()


@pytest.mark.usefixtures('_http_teapot')
Expand Down
Loading