Python 3.3 introduced hash randomization by default. This is a good thing because it avoids us, developer, to relies on the fact that a given hash is most of the time the same like it was the case with Python 2 and until Python 3.3. The “most of the time” assertion have created headache to some developer for example when running doctests on a CI server. We should always assume that dict keys order is random!
For those who don’t know doctest yet, it is a way to:
- document a piece of code: function, method, class, module
- give user a quick view of how to use it
- ensure the documentation reflect with the code as it will be ran against
It looks like this:
When running a test (for example with
py.test --doctest-module) line starting
>>> will be evaluated and return value will be compared with the
Doctest is not intended to be used for huge test case (you should try to split
it anyway) but it allow to get a quick overview of the piece of code. See the
official documentation for more information on usage, don’t miss the
available flags like
NORMALIZE_WHITESPACE which are very
As described in this quick doctest introduction, evaluated call result is compared to the expected one. The problem is that with dict keys will be returned in a random order. A solution could be to compare the sorted keys and then the sorted values but we lost some readability.
Here comes pprint, a built-in pretty print function. It may goal is to print a nice object representation nested list, align dict keys, etc… but it also sort the object which is what we want with the dict in doctests.
Wrapping your dict with pprint will ensure keys order without losing readability and with a minimal performance inpact: