Skip to content

Commit 51fa244

Browse files
committed
Cleaner implementation of #1749.
1 parent d5a70ac commit 51fa244

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

_pytest/doctest.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
from _pytest.fixtures import FixtureRequest
99

1010

11+
DOCTEST_REPORT_CHOICE_NONE = 'none'
12+
DOCTEST_REPORT_CHOICE_CDIFF = 'cdiff'
13+
DOCTEST_REPORT_CHOICE_NDIFF = 'ndiff'
14+
DOCTEST_REPORT_CHOICE_UDIFF = 'udiff'
15+
DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE = 'only_first_failure'
16+
17+
DOCTEST_REPORT_CHOICES = (
18+
DOCTEST_REPORT_CHOICE_NONE,
19+
DOCTEST_REPORT_CHOICE_CDIFF,
20+
DOCTEST_REPORT_CHOICE_NDIFF,
21+
DOCTEST_REPORT_CHOICE_UDIFF,
22+
DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE,
23+
)
1124

1225
def pytest_addoption(parser):
1326
parser.addini('doctest_optionflags', 'option flags for doctests',
@@ -20,7 +33,7 @@ def pytest_addoption(parser):
2033
group.addoption("--doctest-report",
2134
type=str.lower, default="udiff",
2235
help="choose another output format for diffs on doctest failure",
23-
choices=sorted(_get_report_choices_keys()),
36+
choices=DOCTEST_REPORT_CHOICES,
2437
dest="doctestreport")
2538
group.addoption("--doctest-glob",
2639
action="append", default=[], metavar="pat",
@@ -98,7 +111,7 @@ def repr_failure(self, excinfo):
98111
message = excinfo.type.__name__
99112
reprlocation = ReprFileLocation(filename, lineno, message)
100113
checker = _get_checker()
101-
REPORT_UDIFF = _get_report_choices().get(self.config.getoption("doctestreport"))
114+
REPORT_UDIFF = _get_report_choice(self.config.getoption("doctestreport"))
102115
if lineno is not None:
103116
lines = doctestfailure.test.docstring.splitlines(False)
104117
# add line numbers to the left of the error message
@@ -295,25 +308,20 @@ def _get_allow_bytes_flag():
295308
return doctest.register_optionflag('ALLOW_BYTES')
296309

297310

298-
def _get_report_choices_keys():
311+
def _get_report_choice(key):
299312
"""
300-
Returns the report choices keys. Separate function (see just below) because this is used to declare options and
301-
we can't import the doctest module at that time.
302-
"""
303-
return ('udiff', 'cdiff', 'ndiff', 'only_first_failure', 'none', )
304-
305-
def _get_report_choices():
306-
"""
307-
See `_get_report_choices_keys` to understand why this strange implementation. Be careful with order, the values
308-
order should be the same as the returned keys above.
313+
This function returns the actual `doctest` module flag value, we want to do it as late as possible to avoid
314+
importing `doctest` and all its dependencies when parsing options, as it adds overhead and breaks tests.
309315
"""
310316
import doctest
311-
return dict(
312-
zip(
313-
_get_report_choices_keys(),
314-
(doctest.REPORT_UDIFF, doctest.REPORT_CDIFF, doctest.REPORT_NDIFF, doctest.REPORT_ONLY_FIRST_FAILURE, 0, )
315-
)
316-
)
317+
318+
return {
319+
DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF,
320+
DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF,
321+
DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF,
322+
DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE,
323+
DOCTEST_REPORT_CHOICE_NONE: 0,
324+
}[key]
317325

318326
@pytest.fixture(scope='session')
319327
def doctest_namespace():

0 commit comments

Comments
 (0)