diff options
Diffstat (limited to 'sphinx/ext/doctest.py')
-rw-r--r-- | sphinx/ext/doctest.py | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index 20afa2ec6..242356b61 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -13,10 +13,10 @@ import doctest import re import sys import time -import warnings from io import StringIO from os import path -from typing import Any, Callable, Dict, Iterable, List, Sequence, Set, Tuple +from typing import (TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Sequence, Set, Tuple, + Type) from docutils import nodes from docutils.nodes import Element, Node, TextElement @@ -26,17 +26,14 @@ from packaging.version import Version import sphinx from sphinx.builders import Builder -from sphinx.deprecation import RemovedInSphinx40Warning from sphinx.locale import __ from sphinx.util import logging from sphinx.util.console import bold # type: ignore from sphinx.util.docutils import SphinxDirective from sphinx.util.osutil import relpath +from sphinx.util.typing import OptionSpec -if False: - # For type annotation - from typing import Type # for python3.5.1 - +if TYPE_CHECKING: from sphinx.application import Sphinx @@ -46,12 +43,6 @@ blankline_re = re.compile(r'^\s*<BLANKLINE>', re.MULTILINE) doctestopt_re = re.compile(r'#\s*doctest:.+$', re.MULTILINE) -def doctest_encode(text: str, encoding: str) -> str: - warnings.warn('doctest_encode() is deprecated.', - RemovedInSphinx40Warning, stacklevel=2) - return text - - def is_allowed_version(spec: str, version: str) -> bool: """Check `spec` satisfies `version` or not. @@ -96,7 +87,7 @@ class TestDirective(SphinxDirective): if not test: test = code code = doctestopt_re.sub('', code) - nodetype = nodes.literal_block # type: Type[TextElement] + nodetype: Type[TextElement] = nodes.literal_block if self.name in ('testsetup', 'testcleanup') or 'hide' in self.options: nodetype = nodes.comment if self.arguments: @@ -160,15 +151,19 @@ class TestDirective(SphinxDirective): class TestsetupDirective(TestDirective): - option_spec = {'skipif': directives.unchanged_required} # type: Dict + option_spec: OptionSpec = { + 'skipif': directives.unchanged_required + } class TestcleanupDirective(TestDirective): - option_spec = {'skipif': directives.unchanged_required} # type: Dict + option_spec: OptionSpec = { + 'skipif': directives.unchanged_required + } class DoctestDirective(TestDirective): - option_spec = { + option_spec: OptionSpec = { 'hide': directives.flag, 'no-trim-doctest-flags': directives.flag, 'options': directives.unchanged, @@ -179,7 +174,7 @@ class DoctestDirective(TestDirective): class TestcodeDirective(TestDirective): - option_spec = { + option_spec: OptionSpec = { 'hide': directives.flag, 'no-trim-doctest-flags': directives.flag, 'pyversion': directives.unchanged_required, @@ -189,7 +184,7 @@ class TestcodeDirective(TestDirective): class TestoutputDirective(TestDirective): - option_spec = { + option_spec: OptionSpec = { 'hide': directives.flag, 'no-trim-doctest-flags': directives.flag, 'options': directives.unchanged, @@ -207,9 +202,9 @@ parser = doctest.DocTestParser() class TestGroup: def __init__(self, name: str) -> None: self.name = name - self.setup = [] # type: List[TestCode] - self.tests = [] # type: List[List[TestCode]] - self.cleanup = [] # type: List[TestCode] + self.setup: List[TestCode] = [] + self.tests: List[List[TestCode]] = [] + self.cleanup: List[TestCode] = [] def add_code(self, code: "TestCode", prepend: bool = False) -> None: if code.type == 'testsetup': @@ -397,7 +392,7 @@ Doctest summary return False else: condition = node['skipif'] - context = {} # type: Dict[str, Any] + context: Dict[str, Any] = {} if self.config.doctest_global_setup: exec(self.config.doctest_global_setup, context) should_skip = eval(condition, context) @@ -406,7 +401,7 @@ Doctest summary return should_skip def test_doc(self, docname: str, doctree: Node) -> None: - groups = {} # type: Dict[str, TestGroup] + groups: Dict[str, TestGroup] = {} add_to_all_groups = [] self.setup_runner = SphinxDocTestRunner(verbose=False, optionflags=self.opt) @@ -487,7 +482,7 @@ Doctest summary return compile(code, name, self.type, flags, dont_inherit) def test_group(self, group: TestGroup) -> None: - ns = {} # type: Dict + ns: Dict = {} def run_setup_cleanup(runner: Any, testcodes: List[TestCode], what: Any) -> bool: examples = [] |