diff options
Diffstat (limited to 'sphinx/testing/util.py')
-rw-r--r-- | sphinx/testing/util.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/sphinx/testing/util.py b/sphinx/testing/util.py index 60544cfa3..e55007d80 100644 --- a/sphinx/testing/util.py +++ b/sphinx/testing/util.py @@ -18,14 +18,16 @@ from docutils import nodes from docutils.parsers.rst import directives, roles from six import string_types -from sphinx import application +from sphinx import application, locale from sphinx.builders.latex import LaTeXBuilder from sphinx.ext.autodoc import AutoDirective from sphinx.pycode import ModuleAnalyzer from sphinx.testing.path import path if False: + # For type annotation from typing import List # NOQA + from typing import Any, Dict, Generator, IO, List, Pattern # NOQA __all__ = [ @@ -36,21 +38,25 @@ __all__ = [ def assert_re_search(regex, text, flags=0): + # type: (Pattern, unicode, int) -> None if not re.search(regex, text, flags): assert False, '%r did not match %r' % (regex, text) def assert_not_re_search(regex, text, flags=0): + # type: (Pattern, unicode, int) -> None if re.search(regex, text, flags): assert False, '%r did match %r' % (regex, text) def assert_startswith(thing, prefix): + # type: (unicode, unicode) -> None if not thing.startswith(prefix): assert False, '%r does not start with %r' % (thing, prefix) def assert_node(node, cls=None, xpath="", **kwargs): + # type: (nodes.Node, Any, unicode, Any) -> None if cls: if isinstance(cls, list): assert_node(node, cls[0], xpath=xpath, **kwargs) @@ -80,13 +86,15 @@ def assert_node(node, cls=None, xpath="", **kwargs): def etree_parse(path): + # type: (unicode) -> Any with warnings.catch_warnings(record=False): warnings.filterwarnings("ignore", category=DeprecationWarning) - return ElementTree.parse(path) + return ElementTree.parse(path) # type: ignore class Struct(object): def __init__(self, **kwds): + # type: (Any) -> None self.__dict__.update(kwds) @@ -99,6 +107,7 @@ class SphinxTestApp(application.Sphinx): def __init__(self, buildername='html', srcdir=None, freshenv=False, confoverrides=None, status=None, warning=None, tags=None, docutilsconf=None): + # type: (unicode, path, bool, Dict, IO, IO, unicode, unicode) -> None if docutilsconf is not None: (srcdir / 'docutils.conf').write_text(docutilsconf) @@ -127,7 +136,7 @@ class SphinxTestApp(application.Sphinx): if v.startswith('visit_')) try: - application.Sphinx.__init__(self, srcdir, confdir, outdir, doctreedir, + application.Sphinx.__init__(self, srcdir, confdir, outdir, doctreedir, # type: ignore # NOQA buildername, confoverrides, status, warning, freshenv, warningiserror, tags) except Exception: @@ -135,9 +144,11 @@ class SphinxTestApp(application.Sphinx): raise def cleanup(self, doctrees=False): + # type: (bool) -> None AutoDirective._registry.clear() ModuleAnalyzer.cache.clear() LaTeXBuilder.usepackages = [] + locale.translators.clear() sys.path[:] = self._saved_path sys.modules.pop('autodoc_fodder', None) directives._directives = self._saved_directives @@ -149,6 +160,7 @@ class SphinxTestApp(application.Sphinx): delattr(nodes.GenericNodeVisitor, 'depart_' + method[6:]) def __repr__(self): + # type: () -> str return '<%s buildername=%r>' % (self.__class__.__name__, self.builder.name) @@ -160,13 +172,16 @@ class SphinxTestAppWrapperForSkipBuilding(object): """ def __init__(self, app_): + # type: (SphinxTestApp) -> None self.app = app_ def __getattr__(self, name): + # type: (str) -> Any return getattr(self.app, name) def build(self, *args, **kw): - if not self.app.outdir.listdir(): + # type: (Any, Any) -> None + if not self.app.outdir.listdir(): # type: ignore # if listdir is empty, do build. self.app.build(*args, **kw) # otherwise, we can use built cache @@ -176,16 +191,19 @@ _unicode_literals_re = re.compile(r'u(".*?")|u(\'.*?\')') def remove_unicode_literals(s): - return _unicode_literals_re.sub(lambda x: x.group(1) or x.group(2), s) + # type: (unicode) -> unicode + return _unicode_literals_re.sub(lambda x: x.group(1) or x.group(2), s) # type: ignore def find_files(root, suffix=None): + # type: (unicode, bool) -> Generator for dirpath, dirs, files in os.walk(root, followlinks=True): dirpath = path(dirpath) - for f in [f for f in files if not suffix or f.endswith(suffix)]: + for f in [f for f in files if not suffix or f.endswith(suffix)]: # type: ignore fpath = dirpath / f yield os.path.relpath(fpath, root) def strip_escseq(text): + # type: (unicode) -> unicode return re.sub('\x1b.*?m', '', text) |