Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71adf7e

Browse files
committed
Doctest now examines all docstrings by default. Previously, it would
skip over functions with private names (as indicated by the underscore naming convention). The old default created too much of a risk that user tests were being skipped inadvertently. Note, this change could break code in the unlikely case that someone had intentionally put failing tests in the docstrings of private functions. The breakage is easily fixable by specifying the old behavior when calling testmod() or Tester(). The more likely case is that the silent failure was unintended and that the user needed to be informed so the test could be fixed.
1 parent 853276e commit 71adf7e

3 files changed

Lines changed: 44 additions & 25 deletions

File tree

Doc/lib/libdoctest.tex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,24 @@ \subsection{Which Docstrings Are Examined?}
190190

191191
See \file{docstring.py} for all the details. They're unsurprising: the
192192
module docstring, and all function, class and method docstrings are
193-
searched, with the exception of docstrings attached to objects with private
194-
names. Objects imported into the module are not searched.
193+
searched. Optionally, the tester can be directed to exclude
194+
docstrings attached to objects with private names.
195+
Objects imported into the module are not searched.
195196

196197
In addition, if \code{M.__test__} exists and "is true", it must be a
197198
dict, and each entry maps a (string) name to a function object, class
198199
object, or string. Function and class object docstrings found from
199-
\code{M.__test__} are searched even if the name is private, and
200-
strings are searched directly as if they were docstrings. In output,
201-
a key \code{K} in \code{M.__test__} appears with name
200+
\code{M.__test__} are searched even if the the tester has been
201+
directly to skip over private names in the rest of the module.
202+
In output, a key \code{K} in \code{M.__test__} appears with name
202203

203204
\begin{verbatim}
204205
<name of M>.__test__.K
205206
\end{verbatim}
206207

207208
Any classes found are recursively searched similarly, to test docstrings in
208209
their contained methods and nested classes. While private names reached
209-
from \module{M}'s globals are skipped, all names reached from
210+
from \module{M}'s globals can be optionally skipped, all names reached from
210211
\code{M.__test__} are searched.
211212

212213
\subsection{What's the Execution Context?}

Lib/doctest.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def _test():
4848
+ M.__doc__.
4949
5050
+ f.__doc__ for all functions f in M.__dict__.values(), except those
51-
with private names and those defined in other modules.
51+
defined in other modules.
5252
53-
+ C.__doc__ for all classes C in M.__dict__.values(), except those with
54-
private names and those defined in other modules.
53+
+ C.__doc__ for all classes C in M.__dict__.values(), except those
54+
defined in other modules.
5555
5656
+ If M.__test__ exists and "is true", it must be a dict, and
5757
each entry maps a (string) name to a function object, class object, or
@@ -62,13 +62,16 @@ def _test():
6262
<name of M>.__test__.K
6363
6464
Any classes found are recursively searched similarly, to test docstrings in
65-
their contained methods and nested classes. Private names reached from M's
66-
globals are skipped, but all names reached from M.__test__ are searched.
65+
their contained methods and nested classes. All names reached from
66+
M.__test__ are searched.
6767
68-
By default, a name is considered to be private if it begins with an
69-
underscore (like "_my_func") but doesn't both begin and end with (at least)
70-
two underscores (like "__init__"). You can change the default by passing
71-
your own "isprivate" function to testmod.
68+
Optionally, functions with private names can be skipped (unless listed in
69+
M.__test__) by supplying a function to the "isprivate" argument that will
70+
identify private functions. For convenience, one such function is
71+
supplied. docttest.is_private considers a name to be private if it begins
72+
with an underscore (like "_my_func") but doesn't both begin and end with
73+
(at least) two underscores (like "__init__"). By supplying this function
74+
or your own "isprivate" function to testmod, the behavior can be customized.
7275
7376
If you want to test docstrings in objects with private names too, stuff
7477
them into an M.__test__ dict, or see ADVANCED USAGE below (e.g., pass your
@@ -671,8 +674,10 @@ def __init__(self, mod=None, globs=None, verbose=None,
671674
failures if false; by default, it's true iff "-v" is in sys.argv.
672675
673676
Optional keyword arg "isprivate" specifies a function used to determine
674-
whether a name is private. The default function is doctest.is_private;
675-
see its docs for details.
677+
whether a name is private. The default function is to assume that
678+
no functions are private. The "isprivate" arg may be set to
679+
doctest.is_private in order to skip over functions marked as private
680+
using an underscore naming convention; see its docs for details.
676681
677682
See doctest.testmod docs for the meaning of optionflags.
678683
"""
@@ -691,8 +696,9 @@ def __init__(self, mod=None, globs=None, verbose=None,
691696
verbose = "-v" in sys.argv
692697
self.verbose = verbose
693698

699+
# By default, assume that nothing is private
694700
if isprivate is None:
695-
isprivate = is_private
701+
isprivate = lambda prefix, base: 0
696702
self.isprivate = isprivate
697703

698704
self.optionflags = optionflags
@@ -861,26 +867,26 @@ def rundict(self, d, name, module=None):
861867
862868
Tests that objects outside m1 are excluded:
863869
864-
>>> t = Tester(globs={}, verbose=0)
870+
>>> t = Tester(globs={}, verbose=0, isprivate=is_private)
865871
>>> t.rundict(m1.__dict__, "rundict_test", m1) # _f, f2 and g2 and h2 skipped
866872
(0, 3)
867873
868-
Again, but with a custom isprivate function allowing _f:
874+
Again, but with the default isprivate function allowing _f:
869875
870-
>>> t = Tester(globs={}, verbose=0, isprivate=lambda x,y: 0)
876+
>>> t = Tester(globs={}, verbose=0)
871877
>>> t.rundict(m1.__dict__, "rundict_test_pvt", m1) # Only f2, g2 and h2 skipped
872878
(0, 4)
873879
874880
And once more, not excluding stuff outside m1:
875881
876-
>>> t = Tester(globs={}, verbose=0, isprivate=lambda x,y: 0)
882+
>>> t = Tester(globs={}, verbose=0)
877883
>>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped.
878884
(0, 8)
879885
880886
The exclusion of objects from outside the designated module is
881887
meant to be invoked automagically by testmod.
882888
883-
>>> testmod(m1)
889+
>>> testmod(m1, isprivate=is_private)
884890
(0, 3)
885891
886892
"""
@@ -1070,7 +1076,8 @@ def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None,
10701076
10711077
Test examples in docstrings in functions and classes reachable
10721078
from module m (or the current module if m is not supplied), starting
1073-
with m.__doc__. Private names are skipped.
1079+
with m.__doc__. Unless isprivate is specified, private names
1080+
are not skipped.
10741081
10751082
Also test examples reachable from dict m.__test__ if it exists and is
10761083
not None. m.__dict__ maps names to functions, classes and strings;
@@ -1094,7 +1101,9 @@ def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None,
10941101
10951102
Optional keyword arg "isprivate" specifies a function used to
10961103
determine whether a name is private. The default function is
1097-
doctest.is_private; see its docs for details.
1104+
treat all functions as public. Optionally, "isprivate" can be
1105+
set to doctest.is_private to skip over functions marked as private
1106+
using the underscore naming convention; see its docs for details.
10981107
10991108
Optional keyword arg "report" prints a summary at the end when true,
11001109
else prints nothing at the end. In verbose mode, the summary is

Misc/NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ Extension modules
3838
Library
3939
-------
4040

41+
- doctest now examines all docstrings by default. Previously, it would
42+
skip over functions with private names (as indicated by the underscore
43+
naming convention). The old default created too much of a risk that
44+
user tests were being skipped inadvertently. Note, this change could
45+
break code in the unlikely case that someone had intentionally put
46+
failing tests in the docstrings of private functions. The breakage
47+
is easily fixable by specifying the old behavior when calling testmod()
48+
or Tester().
49+
4150
- Closing a dumbdbm database more than once is now harmless (it used to
4251
raise a nuisance exception on the second close).
4352

0 commit comments

Comments
 (0)