Migrate to py3 style type annotation: sphinx.environment.collectors.dependencies

This commit is contained in:
Takeshi KOMIYA 2019-07-06 12:27:39 +09:00
parent 17c719c8de
commit f076afd920

View File

@ -10,35 +10,30 @@
import os import os
from os import path from os import path
from typing import Any, Dict, Set
from docutils import nodes
from docutils.utils import relative_path from docutils.utils import relative_path
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.environment.collectors import EnvironmentCollector from sphinx.environment.collectors import EnvironmentCollector
from sphinx.util.osutil import fs_encoding from sphinx.util.osutil import fs_encoding
if False:
# For type annotation
from typing import Dict, Set # NOQA
from docutils import nodes # NOQA
from sphinx.sphinx import Sphinx # NOQA
from sphinx.environment import BuildEnvironment # NOQA
class DependenciesCollector(EnvironmentCollector): class DependenciesCollector(EnvironmentCollector):
"""dependencies collector for sphinx.environment.""" """dependencies collector for sphinx.environment."""
def clear_doc(self, app, env, docname): def clear_doc(self, app: Sphinx, env: BuildEnvironment, docname: str) -> None:
# type: (Sphinx, BuildEnvironment, str) -> None
env.dependencies.pop(docname, None) env.dependencies.pop(docname, None)
def merge_other(self, app, env, docnames, other): def merge_other(self, app: Sphinx, env: BuildEnvironment,
# type: (Sphinx, BuildEnvironment, Set[str], BuildEnvironment) -> None docnames: Set[str], other: BuildEnvironment) -> None:
for docname in docnames: for docname in docnames:
if docname in other.dependencies: if docname in other.dependencies:
env.dependencies[docname] = other.dependencies[docname] env.dependencies[docname] = other.dependencies[docname]
def process_doc(self, app, doctree): def process_doc(self, app: Sphinx, doctree: nodes.document) -> None:
# type: (Sphinx, nodes.document) -> None
"""Process docutils-generated dependency info.""" """Process docutils-generated dependency info."""
cwd = os.getcwd() cwd = os.getcwd()
frompath = path.join(path.normpath(app.srcdir), 'dummy') frompath = path.join(path.normpath(app.srcdir), 'dummy')
@ -55,8 +50,7 @@ class DependenciesCollector(EnvironmentCollector):
app.env.dependencies[app.env.docname].add(relpath) app.env.dependencies[app.env.docname].add(relpath)
def setup(app): def setup(app: Sphinx) -> Dict[str, Any]:
# type: (Sphinx) -> Dict
app.add_env_collector(DependenciesCollector) app.add_env_collector(DependenciesCollector)
return { return {