summaryrefslogtreecommitdiff
path: root/sphinx/ext/inheritance_diagram.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2019-07-01 00:58:51 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2019-07-04 01:10:15 +0900
commitdd9d02007c5f2dbeb2a294391b035ff445bae9eb (patch)
tree373eaadf4a49023de60f39833735720f30dcc6fa /sphinx/ext/inheritance_diagram.py
parent3a74ef89b3b386daacd4dfa0752536b3ad9aa402 (diff)
downloadsphinx-git-dd9d02007c5f2dbeb2a294391b035ff445bae9eb.tar.gz
Migrate to py3 style type annotation: sphinx.ext.inheritance_diagram
Diffstat (limited to 'sphinx/ext/inheritance_diagram.py')
-rw-r--r--sphinx/ext/inheritance_diagram.py81
1 files changed, 33 insertions, 48 deletions
diff --git a/sphinx/ext/inheritance_diagram.py b/sphinx/ext/inheritance_diagram.py
index df3ff01ed..f52a990bf 100644
--- a/sphinx/ext/inheritance_diagram.py
+++ b/sphinx/ext/inheritance_diagram.py
@@ -40,27 +40,25 @@ import inspect
import re
import sys
from hashlib import md5
-from typing import Iterable, cast
+from typing import Any, Dict, Iterable, List, Tuple
+from typing import cast
from docutils import nodes
+from docutils.nodes import Node
from docutils.parsers.rst import directives
import sphinx
from sphinx import addnodes
+from sphinx.application import Sphinx
+from sphinx.environment import BuildEnvironment
from sphinx.ext.graphviz import (
graphviz, figure_wrapper,
render_dot_html, render_dot_latex, render_dot_texinfo
)
from sphinx.util.docutils import SphinxDirective
-
-if False:
- # For type annotation
- from typing import Any, Dict, List, Optional, Tuple # NOQA
- from sphinx.application import Sphinx # NOQA
- from sphinx.environment import BuildEnvironment # NOQA
- from sphinx.writers.html import HTMLTranslator # NOQA
- from sphinx.writers.latex import LaTeXTranslator # NOQA
- from sphinx.writers.texinfo import TexinfoTranslator # NOQA
+from sphinx.writers.html import HTMLTranslator
+from sphinx.writers.latex import LaTeXTranslator
+from sphinx.writers.texinfo import TexinfoTranslator
module_sig_re = re.compile(r'''^(?:([\w.]*)\.)? # module names
@@ -68,8 +66,7 @@ module_sig_re = re.compile(r'''^(?:([\w.]*)\.)? # module names
''', re.VERBOSE)
-def try_import(objname):
- # type: (str) -> Any
+def try_import(objname: str) -> Any:
"""Import a object or module using *name* and *currentmodule*.
*name* should be a relative name from *currentmodule* or
a fully-qualified name.
@@ -96,8 +93,7 @@ def try_import(objname):
return None
-def import_classes(name, currmodule):
- # type: (str, str) -> Any
+def import_classes(name: str, currmodule: str) -> Any:
"""Import a class using its fully-qualified *name*."""
target = None
@@ -138,9 +134,9 @@ class InheritanceGraph:
from all the way to the root "object", and then is able to generate a
graphviz dot graph from them.
"""
- def __init__(self, class_names, currmodule, show_builtins=False,
- private_bases=False, parts=0, aliases=None, top_classes=[]):
- # type: (List[str], str, bool, bool, int, Optional[Dict[str, str]], List[Any]) -> None
+ def __init__(self, class_names: List[str], currmodule: str, show_builtins: bool = False,
+ private_bases: bool = False, parts: int = 0, aliases: Dict[str, str] = None,
+ top_classes: List[Any] = []) -> None:
"""*class_names* is a list of child classes to show bases from.
If *show_builtins* is True, then Python builtins will be shown
@@ -154,16 +150,16 @@ class InheritanceGraph:
raise InheritanceException('No classes found for '
'inheritance diagram')
- def _import_classes(self, class_names, currmodule):
- # type: (List[str], str) -> List[Any]
+ def _import_classes(self, class_names: List[str], currmodule: str) -> List[Any]:
"""Import a list of classes."""
classes = [] # type: List[Any]
for name in class_names:
classes.extend(import_classes(name, currmodule))
return classes
- def _class_info(self, classes, show_builtins, private_bases, parts, aliases, top_classes):
- # type: (List[Any], bool, bool, int, Optional[Dict[str, str]], List[Any]) -> List[Tuple[str, str, List[str], str]] # NOQA
+ def _class_info(self, classes: List[Any], show_builtins: bool, private_bases: bool,
+ parts: int, aliases: Dict[str, str], top_classes: List[Any]
+ ) -> List[Tuple[str, str, List[str], str]]:
"""Return name and bases for all classes that are ancestors of
*classes*.
@@ -182,8 +178,7 @@ class InheritanceGraph:
all_classes = {}
py_builtins = vars(builtins).values()
- def recurse(cls):
- # type: (Any) -> None
+ def recurse(cls: Any) -> None:
if not show_builtins and cls in py_builtins:
return
if not private_bases and cls.__name__.startswith('_'):
@@ -222,8 +217,7 @@ class InheritanceGraph:
return list(all_classes.values())
- def class_name(self, cls, parts=0, aliases=None):
- # type: (Any, int, Optional[Dict[str, str]]) -> str
+ def class_name(self, cls: Any, parts: int = 0, aliases: Dict[str, str] = None) -> str:
"""Given a class object, return a fully-qualified name.
This works for things I've tested in matplotlib so far, but may not be
@@ -243,8 +237,7 @@ class InheritanceGraph:
return aliases[result]
return result
- def get_all_class_names(self):
- # type: () -> List[str]
+ def get_all_class_names(self) -> List[str]:
"""Get all of the class names involved in the graph."""
return [fullname for (_, fullname, _, _) in self.class_info]
@@ -266,17 +259,15 @@ class InheritanceGraph:
'style': '"setlinewidth(0.5)"',
}
- def _format_node_attrs(self, attrs):
- # type: (Dict) -> str
+ def _format_node_attrs(self, attrs: Dict) -> str:
return ','.join(['%s=%s' % x for x in sorted(attrs.items())])
- def _format_graph_attrs(self, attrs):
- # type: (Dict) -> str
+ def _format_graph_attrs(self, attrs: Dict) -> str:
return ''.join(['%s=%s;\n' % x for x in sorted(attrs.items())])
- def generate_dot(self, name, urls={}, env=None,
- graph_attrs={}, node_attrs={}, edge_attrs={}):
- # type: (str, Dict, BuildEnvironment, Dict, Dict, Dict) -> str
+ def generate_dot(self, name: str, urls: Dict = {}, env: BuildEnvironment = None,
+ graph_attrs: Dict = {}, node_attrs: Dict = {}, edge_attrs: Dict = {}
+ ) -> str:
"""Generate a graphviz dot graph from the classes that were passed in
to __init__.
@@ -344,8 +335,7 @@ class InheritanceDiagram(SphinxDirective):
'top-classes': directives.unchanged_required,
}
- def run(self):
- # type: () -> List[nodes.Node]
+ def run(self) -> List[Node]:
node = inheritance_diagram()
node.document = self.state.document
class_names = self.arguments[0].split()
@@ -391,14 +381,12 @@ class InheritanceDiagram(SphinxDirective):
return [figure]
-def get_graph_hash(node):
- # type: (inheritance_diagram) -> str
+def get_graph_hash(node: inheritance_diagram) -> str:
encoded = (node['content'] + str(node['parts'])).encode()
return md5(encoded).hexdigest()[-10:]
-def html_visit_inheritance_diagram(self, node):
- # type: (HTMLTranslator, inheritance_diagram) -> None
+def html_visit_inheritance_diagram(self: HTMLTranslator, node: inheritance_diagram) -> None:
"""
Output the graph for HTML. This will insert a PNG with clickable
image map.
@@ -431,8 +419,7 @@ def html_visit_inheritance_diagram(self, node):
raise nodes.SkipNode
-def latex_visit_inheritance_diagram(self, node):
- # type: (LaTeXTranslator, inheritance_diagram) -> None
+def latex_visit_inheritance_diagram(self: LaTeXTranslator, node: inheritance_diagram) -> None:
"""
Output the graph for LaTeX. This will insert a PDF.
"""
@@ -447,8 +434,8 @@ def latex_visit_inheritance_diagram(self, node):
raise nodes.SkipNode
-def texinfo_visit_inheritance_diagram(self, node):
- # type: (TexinfoTranslator, inheritance_diagram) -> None
+def texinfo_visit_inheritance_diagram(self: TexinfoTranslator, node: inheritance_diagram
+ ) -> None:
"""
Output the graph for Texinfo. This will insert a PNG.
"""
@@ -463,13 +450,11 @@ def texinfo_visit_inheritance_diagram(self, node):
raise nodes.SkipNode
-def skip(self, node):
- # type: (nodes.NodeVisitor, inheritance_diagram) -> None
+def skip(self: nodes.NodeVisitor, node: inheritance_diagram) -> None:
raise nodes.SkipNode
-def setup(app):
- # type: (Sphinx) -> Dict[str, Any]
+def setup(app: Sphinx) -> Dict[str, Any]:
app.setup_extension('sphinx.ext.graphviz')
app.add_node(
inheritance_diagram,