[Python-ideas] doctest

Devin Jeanpierre jeanpierreda at gmail.com
Fri Mar 2 19:35:34 CET 2012

On Fri, Mar 2, 2012 at 1:27 PM, Mike Meyer <mwm at mired.org> wrote:
> This thread seems to be centered around making comparisons of results
> with indeterminate ordering (dicts being the prime example) work
> properly. In fact, one proposal was to have doctest call sorted on the
> output to make sure it's right, which was shot down because that's not
> always the correct thing to do.

Aahhh, that was me, and I didn't mean to shoot it down. The right
modification would have been to typecheck for dict/set before you
sort, and then format it like a string, IIRC. But at the time the
function just returned a sorted object.

In principle you can absolutely sort dict literals etc., but I don't
think it's any easier to implement than just parsing them into dict
objects and doing a direct dict comparison, so that's why I object to
it. In addition, it's harder for the test writer, who now has to pay
attention to ordering.

> So the question is - why isn't dealing with this the responsibility of
> the test writer? Yeah, it's not quite the spirit of documentation to
> turn a dictionary into a sorted list in the output, but neither is
> littering the documentation with +LITERAL_EVAL and the like.

The thing about +LITERAL_EVAL and the other flags is that modern
doctest-displaying tools like Sphinx hide the comments, so that you
just see what looks like a regular interpreter session, without any #
doctest: directives.

Because of this, it's in principle possible to have "natural looking"
things, in some cases. But yes, the status quo is that, somehow, you
have to handle this explicitly yourself.

-- Devin

More information about the Python-ideas mailing list