[issue3332] DocTest and dict sort.

Jens Diemer report at bugs.python.org
Thu Jul 10 15:59:54 CEST 2008


New submission from Jens Diemer <bugs.python.org at jensdiemer.de>:

The doctest doesn't work good, if a function returns a dict.

Here a simple example:

def test(d):
    """
    This works:
    >>> test({"A":1, "B":2, "C":3})
    {'A': 1, 'C': 3, 'B': 2}
   
    This failed, because of different dict sort:
    >>> test({"A":1, "B":2, "C":3})
    {'A': 1, 'B': 2, 'C': 3}
   
    The Error messages:

    Failed example:
        test({"A":1, "B":2, "C":3})
    Expected:
        {'A': 1, 'B': 2, 'C': 3}
    Got:
        {'A': 1, 'C': 3, 'B': 2}
    """
    return d


The problem is IMHO that doctest.py [1] OutputChecker.check_output()
does compare the repr() of the dict and not the real dict as data.

One solution: Use eval() to convert the string repr. of the dict into
the real dict:

...
        #-----<add>-----
        try:
            if eval(got) == eval(want):
                return True
        except:
            pass #*pfeif* kein schoener stil, aber pragmatisch
        #-----</add>----
        # We didn't find any match; return false.
        return False

German discuss can be found here:
http://www.python-forum.de/topic-15321.html

[1] http://svn.python.org/view/python/trunk/Lib/doctest.py?view=markup

----------
components: Extension Modules
messages: 69501
nosy: jedie
severity: normal
status: open
title: DocTest and dict sort.
type: behavior
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3332>
_______________________________________


More information about the Python-bugs-list mailing list