Skip to content

Commit acdf4a8

Browse files
authored
Use an explicit tags parameter for global_toctree_for_doc() (#13631)
1 parent 25ab3d7 commit acdf4a8

File tree

10 files changed

+68
-20
lines changed

10 files changed

+68
-20
lines changed

sphinx/builders/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,9 @@ def _write_serial(self, docnames: Sequence[str]) -> None:
772772
len(docnames),
773773
self._app.verbosity,
774774
):
775-
_write_docname(docname, app=self._app, env=self.env, builder=self)
775+
_write_docname(
776+
docname, app=self._app, env=self.env, builder=self, tags=self.tags
777+
)
776778

777779
def _write_parallel(self, docnames: Sequence[str], nproc: int) -> None:
778780
def write_process(docs: list[tuple[str, nodes.document]]) -> None:
@@ -782,7 +784,9 @@ def write_process(docs: list[tuple[str, nodes.document]]) -> None:
782784

783785
# warm up caches/compile templates using the first document
784786
firstname, docnames = docnames[0], docnames[1:]
785-
_write_docname(firstname, app=self._app, env=self.env, builder=self)
787+
_write_docname(
788+
firstname, app=self._app, env=self.env, builder=self, tags=self.tags
789+
)
786790

787791
tasks = ParallelTasks(nproc)
788792
chunks = make_chunks(docnames, nproc)
@@ -804,7 +808,9 @@ def on_chunk_done(args: list[tuple[str, nodes.document]], result: None) -> None:
804808
for chunk in chunks:
805809
arg = []
806810
for docname in chunk:
807-
doctree = self.env.get_and_resolve_doctree(docname, self)
811+
doctree = self.env.get_and_resolve_doctree(
812+
docname, self, tags=self.tags
813+
)
808814
self.write_doc_serialized(docname, doctree)
809815
arg.append((docname, doctree))
810816
tasks.add_task(write_process, arg, on_chunk_done)
@@ -871,11 +877,17 @@ def get_builder_config(self, option: str, default: str) -> Any:
871877

872878

873879
def _write_docname(
874-
docname: str, /, *, app: Sphinx, env: BuildEnvironment, builder: Builder
880+
docname: str,
881+
/,
882+
*,
883+
app: Sphinx,
884+
env: BuildEnvironment,
885+
builder: Builder,
886+
tags: Tags,
875887
) -> None:
876888
"""Write a single document."""
877889
app.phase = BuildPhase.RESOLVING
878-
doctree = env.get_and_resolve_doctree(docname, builder=builder)
890+
doctree = env.get_and_resolve_doctree(docname, builder=builder, tags=tags)
879891
app.phase = BuildPhase.WRITING
880892
builder.write_doc_serialized(docname, doctree)
881893
builder.write_doc(docname, doctree)

sphinx/builders/_epub_base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ def get_toc(self) -> None:
233233
and pre and post files not managed by Sphinx.
234234
"""
235235
doctree = self.env.get_and_resolve_doctree(
236-
self.config.master_doc, self, prune_toctrees=False, includehidden=True
236+
self.config.master_doc,
237+
self,
238+
tags=self.tags,
239+
prune_toctrees=False,
240+
includehidden=True,
237241
)
238242
self.refnodes = self.get_refnodes(doctree, [])
239243
master_dir = Path(self.config.master_doc).parent
@@ -765,7 +769,11 @@ def build_toc(self) -> None:
765769

766770
if self.config.epub_tocscope == 'default':
767771
doctree = self.env.get_and_resolve_doctree(
768-
self.config.root_doc, self, prune_toctrees=False, includehidden=False
772+
self.config.root_doc,
773+
self,
774+
tags=self.tags,
775+
prune_toctrees=False,
776+
includehidden=False,
769777
)
770778
refnodes = self.get_refnodes(doctree, [])
771779
self.toc_add_files(refnodes)

sphinx/builders/epub3.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ def build_navigation_doc(self) -> None:
190190

191191
if self.config.epub_tocscope == 'default':
192192
doctree = self.env.get_and_resolve_doctree(
193-
self.config.root_doc, self, prune_toctrees=False, includehidden=False
193+
self.config.root_doc,
194+
self,
195+
tags=self.tags,
196+
prune_toctrees=False,
197+
includehidden=False,
194198
)
195199
refnodes = self.get_refnodes(doctree, [])
196200
self.toc_add_files(refnodes)

sphinx/builders/html/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ def _get_local_toctree(
10291029
if kwargs.get('maxdepth') == '': # NoQA: PLC1901
10301030
kwargs.pop('maxdepth')
10311031
toctree = global_toctree_for_doc(
1032-
self.env, docname, self, collapse=collapse, **kwargs
1032+
self.env, docname, self, tags=self.tags, collapse=collapse, **kwargs
10331033
)
10341034
return self.render_partial(toctree)['fragment']
10351035

sphinx/builders/singlehtml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _get_local_toctree(
8484
if kwargs.get('maxdepth') == '': # NoQA: PLC1901
8585
kwargs.pop('maxdepth')
8686
toctree = global_toctree_for_doc(
87-
self.env, docname, self, collapse=collapse, **kwargs
87+
self.env, docname, self, tags=self.tags, collapse=collapse, **kwargs
8888
)
8989
return self.render_partial(toctree)['fragment']
9090

@@ -141,7 +141,7 @@ def assemble_toc_fignumbers(
141141
def get_doc_context(self, docname: str, body: str, metatags: str) -> dict[str, Any]:
142142
# no relation links...
143143
toctree = global_toctree_for_doc(
144-
self.env, self.config.root_doc, self, collapse=False
144+
self.env, self.config.root_doc, self, tags=self.tags, collapse=False
145145
)
146146
# if there is no toctree, toc is None
147147
if toctree:

sphinx/environment/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ def get_and_resolve_doctree(
701701
self,
702702
docname: str,
703703
builder: Builder,
704+
*,
705+
tags: Tags,
704706
doctree: nodes.document | None = None,
705707
prune_toctrees: bool = True,
706708
includehidden: bool = False,
@@ -770,7 +772,7 @@ def resolve_toctree(
770772
titles_only=titles_only,
771773
collapse=collapse,
772774
includehidden=includehidden,
773-
tags=builder.tags,
775+
tags=self._tags,
774776
)
775777

776778
def resolve_references(

sphinx/environment/adapters/toctree.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
from __future__ import annotations
44

5+
import warnings
56
from typing import TYPE_CHECKING, TypeVar
67

78
from docutils import nodes
89
from docutils.nodes import Element
910

1011
from sphinx import addnodes
12+
from sphinx.deprecation import RemovedInSphinx10Warning
1113
from sphinx.locale import __
1214
from sphinx.util import logging, url_re
1315
from sphinx.util.matching import Matcher
@@ -69,6 +71,8 @@ def global_toctree_for_doc(
6971
env: BuildEnvironment,
7072
docname: str,
7173
builder: Builder,
74+
*,
75+
tags: Tags = ..., # type: ignore[assignment]
7276
collapse: bool = False,
7377
includehidden: bool = True,
7478
maxdepth: int = 0,
@@ -78,6 +82,15 @@ def global_toctree_for_doc(
7882
7983
This gives the global ToC, with all ancestors and their siblings.
8084
"""
85+
if tags is ...:
86+
warnings.warn(
87+
"'tags' will become a required keyword argument "
88+
'for global_toctree_for_doc() in Sphinx 10.0.',
89+
RemovedInSphinx10Warning,
90+
stacklevel=2,
91+
)
92+
tags = builder.tags
93+
8194
resolved = (
8295
_resolve_toctree(
8396
env,
@@ -89,7 +102,7 @@ def global_toctree_for_doc(
89102
titles_only=titles_only,
90103
collapse=collapse,
91104
includehidden=includehidden,
92-
tags=builder.tags,
105+
tags=tags,
93106
)
94107
for toctree_node in env.master_doctree.findall(addnodes.toctree)
95108
)
@@ -582,5 +595,5 @@ def get_toctree_for(
582595
**kwargs: Any,
583596
) -> Element | None:
584597
return global_toctree_for_doc(
585-
self.env, docname, builder, collapse=collapse, **kwargs
598+
self.env, docname, builder, tags=builder.tags, collapse=collapse, **kwargs
586599
)

tests/test_environment/test_environment_toctree.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,9 @@ def test_document_toc_tocdepth(app):
614614
@pytest.mark.test_params(shared_result='test_environment_toctree_basic')
615615
def test_global_toctree_for_doc(app):
616616
app.build()
617-
toctree = global_toctree_for_doc(app.env, 'index', app.builder, collapse=False)
617+
toctree = global_toctree_for_doc(
618+
app.env, 'index', app.builder, tags=app.tags, collapse=False
619+
)
618620
assert_node(
619621
toctree,
620622
[
@@ -676,7 +678,9 @@ def test_global_toctree_for_doc(app):
676678
@pytest.mark.test_params(shared_result='test_environment_toctree_basic')
677679
def test_global_toctree_for_doc_collapse(app):
678680
app.build()
679-
toctree = global_toctree_for_doc(app.env, 'index', app.builder, collapse=True)
681+
toctree = global_toctree_for_doc(
682+
app.env, 'index', app.builder, tags=app.tags, collapse=True
683+
)
680684
assert_node(
681685
toctree,
682686
[
@@ -723,7 +727,7 @@ def test_global_toctree_for_doc_collapse(app):
723727
def test_global_toctree_for_doc_maxdepth(app):
724728
app.build()
725729
toctree = global_toctree_for_doc(
726-
app.env, 'index', app.builder, collapse=False, maxdepth=3
730+
app.env, 'index', app.builder, tags=app.tags, collapse=False, maxdepth=3
727731
)
728732
assert_node(
729733
toctree,
@@ -814,7 +818,12 @@ def test_global_toctree_for_doc_maxdepth(app):
814818
def test_global_toctree_for_doc_includehidden(app):
815819
app.build()
816820
toctree = global_toctree_for_doc(
817-
app.env, 'index', app.builder, collapse=False, includehidden=False
821+
app.env,
822+
'index',
823+
app.builder,
824+
tags=app.tags,
825+
collapse=False,
826+
includehidden=False,
818827
)
819828
assert_node(
820829
toctree,

tests/test_extensions/test_ext_inheritance_diagram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def test_inheritance_diagram_latex_alias(app):
293293
app.config.inheritance_alias = {'test.Foo': 'alias.Foo'}
294294
app.build(force_all=True)
295295

296-
doc = app.env.get_and_resolve_doctree('index', app)
296+
doc = app.env.get_and_resolve_doctree('index', app.builder, tags=app.tags)
297297
aliased_graph = doc.children[0].children[3]['graph'].class_info
298298
assert len(aliased_graph) == 4
299299
assert (

tests/test_extensions/test_ext_math.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_imgmath_numfig_html(app: SphinxTestApp) -> None:
323323
def test_math_compat(app):
324324
with warnings.catch_warnings(record=True):
325325
app.build(force_all=True)
326-
doctree = app.env.get_and_resolve_doctree('index', app.builder)
326+
doctree = app.env.get_and_resolve_doctree('index', app.builder, tags=app.tags)
327327

328328
assert_node(
329329
doctree,

0 commit comments

Comments
 (0)