Skip to content

Commit 1dbf699

Browse files
gh-80856: doc: reveal doctest directives (GH-92318)
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <[email protected]> Co-authored-by: Ezio Melotti <[email protected]> (cherry picked from commit 7b024e3) Co-authored-by: Davide Rizzo <[email protected]>
1 parent 15cb6e8 commit 1dbf699

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

Doc/library/doctest.rst

+37-17
Original file line numberDiff line numberDiff line change
@@ -715,36 +715,51 @@ above.
715715
An example's doctest directives modify doctest's behavior for that single
716716
example. Use ``+`` to enable the named behavior, or ``-`` to disable it.
717717

718-
For example, this test passes::
718+
For example, this test passes:
719719

720-
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
720+
.. doctest::
721+
:no-trim-doctest-flags:
722+
723+
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
721724
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
722725
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
723726

724727
Without the directive it would fail, both because the actual output doesn't have
725728
two blanks before the single-digit list elements, and because the actual output
726729
is on a single line. This test also passes, and also requires a directive to do
727-
so::
730+
so:
731+
732+
.. doctest::
733+
:no-trim-doctest-flags:
728734

729-
>>> print(list(range(20))) # doctest: +ELLIPSIS
735+
>>> print(list(range(20))) # doctest: +ELLIPSIS
730736
[0, 1, ..., 18, 19]
731737

732738
Multiple directives can be used on a single physical line, separated by
733-
commas::
739+
commas:
734740

735-
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
741+
.. doctest::
742+
:no-trim-doctest-flags:
743+
744+
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
736745
[0, 1, ..., 18, 19]
737746

738747
If multiple directive comments are used for a single example, then they are
739-
combined::
748+
combined:
749+
750+
.. doctest::
751+
:no-trim-doctest-flags:
740752

741-
>>> print(list(range(20))) # doctest: +ELLIPSIS
742-
... # doctest: +NORMALIZE_WHITESPACE
753+
>>> print(list(range(20))) # doctest: +ELLIPSIS
754+
... # doctest: +NORMALIZE_WHITESPACE
743755
[0, 1, ..., 18, 19]
744756

745757
As the previous example shows, you can add ``...`` lines to your example
746758
containing only directives. This can be useful when an example is too long for
747-
a directive to comfortably fit on the same line::
759+
a directive to comfortably fit on the same line:
760+
761+
.. doctest::
762+
:no-trim-doctest-flags:
748763

749764
>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40)))
750765
... # doctest: +ELLIPSIS
@@ -784,18 +799,23 @@ instead. Another is to do ::
784799

785800
There are others, but you get the idea.
786801

787-
Another bad idea is to print things that embed an object address, like ::
802+
Another bad idea is to print things that embed an object address, like
803+
804+
.. doctest::
788805

789-
>>> id(1.0) # certain to fail some of the time
806+
>>> id(1.0) # certain to fail some of the time # doctest: +SKIP
790807
7948648
791808
>>> class C: pass
792-
>>> C() # the default repr() for instances embeds an address
793-
<__main__.C instance at 0x00AC18F0>
809+
>>> C() # the default repr() for instances embeds an address # doctest: +SKIP
810+
<C object at 0x00AC18F0>
811+
812+
The :const:`ELLIPSIS` directive gives a nice approach for the last example:
794813

795-
The :const:`ELLIPSIS` directive gives a nice approach for the last example::
814+
.. doctest::
815+
:no-trim-doctest-flags:
796816

797-
>>> C() #doctest: +ELLIPSIS
798-
<__main__.C instance at 0x...>
817+
>>> C() # doctest: +ELLIPSIS
818+
<C object at 0x...>
799819

800820
Floating-point numbers are also subject to small output variations across
801821
platforms, because Python defers to the platform C library for float formatting,

0 commit comments

Comments
 (0)