mirror of
https://github.com/python/cpython.git
synced 2025-08-02 08:02:56 +00:00
gh-80856: doc: reveal doctest directives (#92318)
* Doc: Reveal doctest directives. * Fix whitespace. Co-authored-by: Julien Palard <julien@palard.fr> Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
This commit is contained in:
parent
5c3ecddad7
commit
7b024e3a3f
1 changed files with 37 additions and 17 deletions
|
@ -714,36 +714,51 @@ above.
|
||||||
An example's doctest directives modify doctest's behavior for that single
|
An example's doctest directives modify doctest's behavior for that single
|
||||||
example. Use ``+`` to enable the named behavior, or ``-`` to disable it.
|
example. Use ``+`` to enable the named behavior, or ``-`` to disable it.
|
||||||
|
|
||||||
For example, this test passes::
|
For example, this test passes:
|
||||||
|
|
||||||
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
|
.. doctest::
|
||||||
|
:no-trim-doctest-flags:
|
||||||
|
|
||||||
|
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
|
||||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
||||||
|
|
||||||
Without the directive it would fail, both because the actual output doesn't have
|
Without the directive it would fail, both because the actual output doesn't have
|
||||||
two blanks before the single-digit list elements, and because the actual output
|
two blanks before the single-digit list elements, and because the actual output
|
||||||
is on a single line. This test also passes, and also requires a directive to do
|
is on a single line. This test also passes, and also requires a directive to do
|
||||||
so::
|
so:
|
||||||
|
|
||||||
>>> print(list(range(20))) # doctest: +ELLIPSIS
|
.. doctest::
|
||||||
|
:no-trim-doctest-flags:
|
||||||
|
|
||||||
|
>>> print(list(range(20))) # doctest: +ELLIPSIS
|
||||||
[0, 1, ..., 18, 19]
|
[0, 1, ..., 18, 19]
|
||||||
|
|
||||||
Multiple directives can be used on a single physical line, separated by
|
Multiple directives can be used on a single physical line, separated by
|
||||||
commas::
|
commas:
|
||||||
|
|
||||||
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
|
.. doctest::
|
||||||
|
:no-trim-doctest-flags:
|
||||||
|
|
||||||
|
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
|
||||||
[0, 1, ..., 18, 19]
|
[0, 1, ..., 18, 19]
|
||||||
|
|
||||||
If multiple directive comments are used for a single example, then they are
|
If multiple directive comments are used for a single example, then they are
|
||||||
combined::
|
combined:
|
||||||
|
|
||||||
>>> print(list(range(20))) # doctest: +ELLIPSIS
|
.. doctest::
|
||||||
... # doctest: +NORMALIZE_WHITESPACE
|
:no-trim-doctest-flags:
|
||||||
|
|
||||||
|
>>> print(list(range(20))) # doctest: +ELLIPSIS
|
||||||
|
... # doctest: +NORMALIZE_WHITESPACE
|
||||||
[0, 1, ..., 18, 19]
|
[0, 1, ..., 18, 19]
|
||||||
|
|
||||||
As the previous example shows, you can add ``...`` lines to your example
|
As the previous example shows, you can add ``...`` lines to your example
|
||||||
containing only directives. This can be useful when an example is too long for
|
containing only directives. This can be useful when an example is too long for
|
||||||
a directive to comfortably fit on the same line::
|
a directive to comfortably fit on the same line:
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
:no-trim-doctest-flags:
|
||||||
|
|
||||||
>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40)))
|
>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40)))
|
||||||
... # doctest: +ELLIPSIS
|
... # doctest: +ELLIPSIS
|
||||||
|
@ -783,18 +798,23 @@ instead. Another is to do ::
|
||||||
|
|
||||||
There are others, but you get the idea.
|
There are others, but you get the idea.
|
||||||
|
|
||||||
Another bad idea is to print things that embed an object address, like ::
|
Another bad idea is to print things that embed an object address, like
|
||||||
|
|
||||||
>>> id(1.0) # certain to fail some of the time
|
.. doctest::
|
||||||
|
|
||||||
|
>>> id(1.0) # certain to fail some of the time # doctest: +SKIP
|
||||||
7948648
|
7948648
|
||||||
>>> class C: pass
|
>>> class C: pass
|
||||||
>>> C() # the default repr() for instances embeds an address
|
>>> C() # the default repr() for instances embeds an address # doctest: +SKIP
|
||||||
<__main__.C instance at 0x00AC18F0>
|
<C object at 0x00AC18F0>
|
||||||
|
|
||||||
The :const:`ELLIPSIS` directive gives a nice approach for the last example::
|
The :const:`ELLIPSIS` directive gives a nice approach for the last example:
|
||||||
|
|
||||||
>>> C() #doctest: +ELLIPSIS
|
.. doctest::
|
||||||
<__main__.C instance at 0x...>
|
:no-trim-doctest-flags:
|
||||||
|
|
||||||
|
>>> C() # doctest: +ELLIPSIS
|
||||||
|
<C object at 0x...>
|
||||||
|
|
||||||
Floating-point numbers are also subject to small output variations across
|
Floating-point numbers are also subject to small output variations across
|
||||||
platforms, because Python defers to the platform C library for float formatting,
|
platforms, because Python defers to the platform C library for float formatting,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue