Skip to content

Commit c8a10d2

Browse files
authored
bpo-36675: Doc: Reveal doctest directives (pythonGH-23620)
1 parent 3ca2b8f commit c8a10d2

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

.azure-pipelines/docs-steps.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ steps:
1212
inputs:
1313
versionSpec: '>=3.6'
1414

15-
- script: python -m pip install sphinx==2.2.0 blurb python-docs-theme
15+
- script: python -m pip install sphinx==3.2.1 blurb python-docs-theme
1616
displayName: 'Install build dependencies'
1717

1818
- ${{ if ne(parameters.latex, 'true') }}:
@@ -31,7 +31,7 @@ steps:
3131
- ${{ if eq(parameters.upload, 'true') }}:
3232
- task: PublishBuildArtifacts@1
3333
displayName: 'Publish docs'
34-
34+
3535
inputs:
3636
PathToPublish: '$(build.sourcesDirectory)/Doc/build'
3737
ArtifactName: docs

Doc/library/doctest.rst

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -719,36 +719,51 @@ above.
719719
An example's doctest directives modify doctest's behavior for that single
720720
example. Use ``+`` to enable the named behavior, or ``-`` to disable it.
721721

722-
For example, this test passes::
722+
For example, this test passes:
723723

724-
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
724+
.. doctest::
725+
:no-trim-doctest-flags:
726+
727+
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
725728
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
726729
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
727730

728731
Without the directive it would fail, both because the actual output doesn't have
729732
two blanks before the single-digit list elements, and because the actual output
730733
is on a single line. This test also passes, and also requires a directive to do
731-
so::
734+
so:
735+
736+
.. doctest::
737+
:no-trim-doctest-flags:
732738

733-
>>> print(list(range(20))) # doctest: +ELLIPSIS
739+
>>> print(list(range(20))) # doctest: +ELLIPSIS
734740
[0, 1, ..., 18, 19]
735741

736742
Multiple directives can be used on a single physical line, separated by
737-
commas::
743+
commas:
738744

739-
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
745+
.. doctest::
746+
:no-trim-doctest-flags:
747+
748+
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
740749
[0, 1, ..., 18, 19]
741750

742751
If multiple directive comments are used for a single example, then they are
743-
combined::
752+
combined:
753+
754+
.. doctest::
755+
:no-trim-doctest-flags:
744756

745-
>>> print(list(range(20))) # doctest: +ELLIPSIS
746-
... # doctest: +NORMALIZE_WHITESPACE
757+
>>> print(list(range(20))) # doctest: +ELLIPSIS
758+
... # doctest: +NORMALIZE_WHITESPACE
747759
[0, 1, ..., 18, 19]
748760

749761
As the previous example shows, you can add ``...`` lines to your example
750762
containing only directives. This can be useful when an example is too long for
751-
a directive to comfortably fit on the same line::
763+
a directive to comfortably fit on the same line:
764+
765+
.. doctest::
766+
:no-trim-doctest-flags:
752767

753768
>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40)))
754769
... # doctest: +ELLIPSIS
@@ -793,18 +808,23 @@ instead. Another is to do ::
793808

794809
There are others, but you get the idea.
795810

796-
Another bad idea is to print things that embed an object address, like ::
811+
Another bad idea is to print things that embed an object address, like
812+
813+
.. doctest::
797814

798-
>>> id(1.0) # certain to fail some of the time
815+
>>> id(1.0) # certain to fail some of the time # doctest: +SKIP
799816
7948648
800817
>>> class C: pass
801-
>>> C() # the default repr() for instances embeds an address
802-
<__main__.C instance at 0x00AC18F0>
818+
>>> C() # the default repr() for instances embeds an address # doctest: +SKIP
819+
<C object at 0x00AC18F0>
820+
821+
The :const:`ELLIPSIS` directive gives a nice approach for the last example:
803822

804-
The :const:`ELLIPSIS` directive gives a nice approach for the last example::
823+
.. doctest::
824+
:no-trim-doctest-flags:
805825

806-
>>> C() #doctest: +ELLIPSIS
807-
<__main__.C instance at 0x...>
826+
>>> C() # doctest: +ELLIPSIS
827+
<C object at 0x...>
808828

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

0 commit comments

Comments
 (0)
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