From 493578485ab4e04379076fa66465ce2516e0bac5 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Thu, 3 Dec 2020 00:06:51 +0100 Subject: [PATCH 1/2] Doc: Reveal doctest directives. --- Doc/library/doctest.rst | 54 ++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst index 0bbb640bea26b1..235e80c9af42e2 100644 --- a/Doc/library/doctest.rst +++ b/Doc/library/doctest.rst @@ -718,36 +718,51 @@ above. An example's doctest directives modify doctest's behavior for that single 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, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 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 is on a single line. This test also passes, and also requires a directive to do -so:: +so: + +.. doctest:: + :no-trim-doctest-flags: - >>> print(list(range(20))) # doctest: +ELLIPSIS + >>> print(list(range(20))) # doctest: +ELLIPSIS [0, 1, ..., 18, 19] 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] If multiple directive comments are used for a single example, then they are -combined:: +combined: + +.. doctest:: + :no-trim-doctest-flags: - >>> print(list(range(20))) # doctest: +ELLIPSIS - ... # doctest: +NORMALIZE_WHITESPACE + >>> print(list(range(20))) # doctest: +ELLIPSIS + ... # doctest: +NORMALIZE_WHITESPACE [0, 1, ..., 18, 19] 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 -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))) ... # doctest: +ELLIPSIS @@ -792,18 +807,23 @@ instead. Another is to do :: 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 + +.. doctest:: - >>> id(1.0) # certain to fail some of the time + >>> id(1.0) # certain to fail some of the time # doctest: +SKIP 7948648 >>> class C: pass - >>> C() # the default repr() for instances embeds an address - <__main__.C instance at 0x00AC18F0> + >>> C() # the default repr() for instances embeds an address # doctest: +SKIP + + +The :const:`ELLIPSIS` directive gives a nice approach for the last example: -The :const:`ELLIPSIS` directive gives a nice approach for the last example:: +.. doctest:: + :no-trim-doctest-flags: - >>> C() #doctest: +ELLIPSIS - <__main__.C instance at 0x...> + >>> C() # doctest: +ELLIPSIS + Floating-point numbers are also subject to small output variations across platforms, because Python defers to the platform C library for float formatting, From 8e10cd7e258a428cbd049678aa3dc21095c233cf Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sat, 7 May 2022 09:16:40 +0200 Subject: [PATCH 2/2] Fix whitespace. --- Doc/library/doctest.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst index 235e80c9af42e2..8410b95716c837 100644 --- a/Doc/library/doctest.rst +++ b/Doc/library/doctest.rst @@ -811,10 +811,10 @@ Another bad idea is to print things that embed an object address, like .. doctest:: - >>> id(1.0) # certain to fail some of the time # doctest: +SKIP + >>> id(1.0) # certain to fail some of the time # doctest: +SKIP 7948648 >>> class C: pass - >>> C() # the default repr() for instances embeds an address # doctest: +SKIP + >>> C() # the default repr() for instances embeds an address # doctest: +SKIP The :const:`ELLIPSIS` directive gives a nice approach for the last example: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy