8
8
from _pytest .fixtures import FixtureRequest
9
9
10
10
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
+ )
11
24
12
25
def pytest_addoption (parser ):
13
26
parser .addini ('doctest_optionflags' , 'option flags for doctests' ,
@@ -20,7 +33,7 @@ def pytest_addoption(parser):
20
33
group .addoption ("--doctest-report" ,
21
34
type = str .lower , default = "udiff" ,
22
35
help = "choose another output format for diffs on doctest failure" ,
23
- choices = sorted ( _get_report_choices_keys ()) ,
36
+ choices = DOCTEST_REPORT_CHOICES ,
24
37
dest = "doctestreport" )
25
38
group .addoption ("--doctest-glob" ,
26
39
action = "append" , default = [], metavar = "pat" ,
@@ -98,7 +111,7 @@ def repr_failure(self, excinfo):
98
111
message = excinfo .type .__name__
99
112
reprlocation = ReprFileLocation (filename , lineno , message )
100
113
checker = _get_checker ()
101
- REPORT_UDIFF = _get_report_choices (). get (self .config .getoption ("doctestreport" ))
114
+ REPORT_UDIFF = _get_report_choice (self .config .getoption ("doctestreport" ))
102
115
if lineno is not None :
103
116
lines = doctestfailure .test .docstring .splitlines (False )
104
117
# add line numbers to the left of the error message
@@ -295,25 +308,20 @@ def _get_allow_bytes_flag():
295
308
return doctest .register_optionflag ('ALLOW_BYTES' )
296
309
297
310
298
- def _get_report_choices_keys ( ):
311
+ def _get_report_choice ( key ):
299
312
"""
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.
309
315
"""
310
316
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 ]
317
325
318
326
@pytest .fixture (scope = 'session' )
319
327
def doctest_namespace ():
0 commit comments