Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Doc/library/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -771,23 +771,27 @@ Warnings
:mod:`doctest` is serious about requiring exact matches in expected output. If
even a single character doesn't match, the test fails. This will probably
surprise you a few times, as you learn exactly what Python does and doesn't
guarantee about output. For example, when printing a dict, Python doesn't
guarantee that the key-value pairs will be printed in any particular order, so a
test like ::
guarantee about output. For example, when printing a set, Python doesn't
guarantee that the element is printed in any particular order, so a test like ::

>>> foo()
{"Hermione": "hippogryph", "Harry": "broomstick"}
{"Hermione", "Harry"}

is vulnerable! One workaround is to do ::

>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}
>>> foo() == {"Hermione", "Harry"}
True

instead. Another is to do ::

>>> d = sorted(foo().items())
>>> d = sorted(foo())
>>> d
[('Harry', 'broomstick'), ('Hermione', 'hippogryph')]
['Harry', 'Hermione']

.. note::

Before Python 3.6, when printing a dict, Python did not guarantee that
the key-value pairs was printed in any particular order.

There are others, but you get the idea.

Expand Down