summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2018-12-10 23:41:21 +0900
committerGitHub <noreply@github.com>2018-12-10 23:41:21 +0900
commit41ee4cf5f96f3f7f7994e66c881fbce7da1a3af0 (patch)
tree143ecebf19cd51848152c4b295e5396eb5e8b42a
parent9fc1e1101b045371615e59f25dfe40ef707639a1 (diff)
parentfd47f3baf714c84b59548a20ffe24cdd14f2d29a (diff)
downloadsphinx-git-41ee4cf5f96f3f7f7994e66c881fbce7da1a3af0.tar.gz
Merge pull request #5753 from tk0miya/fix_typehints_for_toctree
Fix annotations for toctree
-rw-r--r--sphinx/environment/adapters/toctree.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/sphinx/environment/adapters/toctree.py b/sphinx/environment/adapters/toctree.py
index 6ae720eb6..5d5a9b940 100644
--- a/sphinx/environment/adapters/toctree.py
+++ b/sphinx/environment/adapters/toctree.py
@@ -9,6 +9,8 @@
:license: BSD, see LICENSE for details.
"""
+from typing import Iterable, cast
+
from docutils import nodes
from sphinx import addnodes
@@ -50,7 +52,7 @@ class TocTree:
def resolve(self, docname, builder, toctree, prune=True, maxdepth=0,
titles_only=False, collapse=False, includehidden=False):
- # type: (unicode, Builder, addnodes.toctree, bool, int, bool, bool, bool) -> nodes.Node
+ # type: (unicode, Builder, addnodes.toctree, bool, int, bool, bool, bool) -> nodes.Element # NOQA
"""Resolve a *toctree* node into individual bullet lists with titles
as items, returning None (if no containing titles are found) or
a new node.
@@ -118,10 +120,10 @@ class TocTree:
subnode = subnode.parent
def _entries_from_toctree(toctreenode, parents, separate=False, subtree=False):
- # type: (addnodes.toctree, List[nodes.Node], bool, bool) -> List[nodes.Node]
+ # type: (addnodes.toctree, List[unicode], bool, bool) -> List[nodes.Element]
"""Return TOC entries for a toctree node."""
refs = [(e[0], e[1]) for e in toctreenode['entries']]
- entries = []
+ entries = [] # type: List[nodes.Element]
for (title, ref) in refs:
try:
refdoc = None
@@ -184,9 +186,15 @@ class TocTree:
# if titles_only is given, only keep the main title and
# sub-toctrees
if titles_only:
+ # children of toc are:
+ # - list_item + compact_paragraph + (reference and subtoc)
+ # - only + subtoc
+ # - toctree
+ children = cast(Iterable[nodes.Element], toc)
+
# delete everything but the toplevel title(s)
# and toctrees
- for toplevel in toc:
+ for toplevel in children:
# nodes with length 1 don't have any children anyway
if len(toplevel) > 1:
subtrees = toplevel.traverse(addnodes.toctree)
@@ -199,16 +207,17 @@ class TocTree:
if not (subtocnode.get('hidden', False) and
not includehidden):
i = subtocnode.parent.index(subtocnode) + 1
- for item in _entries_from_toctree(
+ for entry in _entries_from_toctree(
subtocnode, [refdoc] + parents,
subtree=True):
- subtocnode.parent.insert(i, item)
+ subtocnode.parent.insert(i, entry)
i += 1
subtocnode.parent.remove(subtocnode)
if separate:
entries.append(toc)
else:
- entries.extend(toc.children)
+ children = cast(Iterable[nodes.Element], toc)
+ entries.extend(children)
if not subtree and not separate:
ret = nodes.bullet_list()
ret += entries
@@ -247,7 +256,7 @@ class TocTree:
_toctree_add_classes(newnode, 1)
self._toctree_prune(newnode, 1, prune and maxdepth or 0, collapse)
- if len(newnode[-1]) == 0: # No titles found
+ if isinstance(newnode[-1], nodes.Element) and len(newnode[-1]) == 0: # No titles found
return None
# set the target paths in the toctrees (they are not known at TOC
@@ -310,10 +319,10 @@ class TocTree:
return toc
def get_toctree_for(self, docname, builder, collapse, **kwds):
- # type: (unicode, Builder, bool, Any) -> nodes.Node
+ # type: (unicode, Builder, bool, Any) -> nodes.Element
"""Return the global TOC nodetree."""
doctree = self.env.get_doctree(self.env.config.master_doc)
- toctrees = [] # type: List[addnodes.toctree]
+ toctrees = [] # type: List[nodes.Element]
if 'includehidden' not in kwds:
kwds['includehidden'] = True
if 'maxdepth' not in kwds: