Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue3332] DocTest and dict sort.

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Jul 10, 2008, 6:59 AM

Post #1 of 3 (265 views)
Permalink
[issue3332] DocTest and dict sort.

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

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>
<http://bugs.python.org/issue3332>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 10, 2008, 7:06 AM

Post #2 of 3 (235 views)
Permalink
[issue3332] DocTest and dict sort. [In reply to]

Tarek Ziadé <ziade.tarek [at] gmail> added the comment:

This is not a bug: dict do not guarantee the ordering of the keys, so it
may change on every run.

A good practice is to test a sorted .items() output of your dict in your
doctest.

----------
nosy: +tarek

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue3332>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 10, 2008, 7:13 AM

Post #3 of 3 (241 views)
Permalink
[issue3332] DocTest and dict sort. [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

Closing as not a bug.

FWIW, here's the relevant text from the docs:
---------------------------------------------
23.2.3.6 Warnings
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


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

is vulnerable! One workaround is to do


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

instead. Another is to do


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

----------
nosy: +rhettinger
resolution: -> invalid
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue3332>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.