
On Thu, Mar 1, 2012 at 9:31 PM, Mark Janssen <dreamingforward@gmail.com> wrote:
I just thought of something: isn't the obvious solution is for doctest to test the type of an expression's output and if it's in the set of unordered types {set, dict} to run sort() on it? Then the docstring author can just put the (sorted) output of what's expected....
It's the solution people seem to think of first, so it's definitely fairly obvious. It's not a big improvement, though. The original problem is that dict order shouldn't matter, but in doctest it does, making dicts unusable in the normal doctest style. Making it a specific dict order be the prominent one lets you use dicts in doctest, but you have to sort the dicts and rewrite the doctest to take that into account. And even so, some dicts cannot be represented this way. For example, the dict {1:2, "hello": world} cannot be sorted in Python 3, so it won't work in this scheme. The solution I used was to ast.literal_eval on both sides every time you compare output. This way you don't have to care about dict order, or whitespace, or any of these things for Python objects. (There is a flag for not caring about whitespace, but this would inappropriately collapse e.g. the whitespace inside string literals inside a dict. So imo this kills two birds with one stone). -- Devin