Merge pull request #6547 from ngoldbaum/issue-6545

Fix #6545. Strip doctests for doctest_block nodes.
This commit is contained in:
Takeshi KOMIYA 2019-07-09 00:14:49 +09:00 committed by GitHub
commit 632223b860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 8 deletions

View File

@ -9,7 +9,7 @@
""" """
import sys import sys
from typing import NamedTuple from typing import NamedTuple, Union
from docutils import nodes from docutils import nodes
from pygments.lexers import PythonConsoleLexer, guess_lexer from pygments.lexers import PythonConsoleLexer, guess_lexer
@ -110,8 +110,16 @@ class TrimDoctestFlagsTransform(SphinxTransform):
if not self.config.trim_doctest_flags: if not self.config.trim_doctest_flags:
return return
for node in self.document.traverse(nodes.literal_block): for lbnode in self.document.traverse(nodes.literal_block): # type: nodes.literal_block
if self.is_pyconsole(node): if self.is_pyconsole(lbnode):
self.strip_doctest_flags(lbnode)
for dbnode in self.document.traverse(nodes.doctest_block): # type: nodes.doctest_block
self.strip_doctest_flags(dbnode)
@staticmethod
def strip_doctest_flags(node):
# type: (Union[nodes.literal_block, nodes.doctest_block]) -> None
source = node.rawsource source = node.rawsource
source = doctest.blankline_re.sub('', source) source = doctest.blankline_re.sub('', source)
source = doctest.doctestopt_re.sub('', source) source = doctest.doctestopt_re.sub('', source)

View File

@ -21,3 +21,8 @@ test-trim_doctest_flags
>>> datetime.date.now() # doctest: +QUX >>> datetime.date.now() # doctest: +QUX
datetime.date(2008, 1, 1) datetime.date(2008, 1, 1)
.. doctest_block::
>>> datetime.date.now() # doctest: +QUUX
datetime.date(2008, 1, 1)

View File

@ -18,6 +18,7 @@ def test_trim_doctest_flags_html(app, status, warning):
assert 'BAR' in result assert 'BAR' in result
assert 'BAZ' not in result assert 'BAZ' not in result
assert 'QUX' not in result assert 'QUX' not in result
assert 'QUUX' not in result
@pytest.mark.sphinx('latex', testroot='trim_doctest_flags') @pytest.mark.sphinx('latex', testroot='trim_doctest_flags')
@ -29,3 +30,4 @@ def test_trim_doctest_flags_latex(app, status, warning):
assert 'BAR' in result assert 'BAR' in result
assert 'BAZ' not in result assert 'BAZ' not in result
assert 'QUX' not in result assert 'QUX' not in result
assert 'QUUX' not in result