This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PrettyPrinter cannot print dicts with unsortable keys
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: LambertDW, MartinAltmayer, r.david.murray
Priority: normal Keywords:

Created on 2009-12-03 18:30 by MartinAltmayer, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg95939 - (view) Author: Martin Altmayer (MartinAltmayer) * Date: 2009-12-03 18:30
In the following code I use a class for dictionary-keys that has a
__hash__-function but cannot be ordered and try to print that dictionary
with a PrettyPrinter.

import pprint
pp = pprint.PrettyPrinter()

# A class that supports hashing and comparison for equality but cannot
be ordered
class KeyClass:
    def __init__(self,id):
        self.id = id
        
    def __eq__(self,other):
        return self.id == other.id
        
    def __ne__(self,other):
        return self.id != other.id
        
    def __hash__(self):
        return self.id
    

dictionary = dict.fromkeys([KeyClass(i) for i in range(10)])
pp.pprint(dictionary)

The script crashes with the following errors:

Traceback (most recent call last):
  File "/usr/local/lib/python3.1/pprint.py", line 272, in _safe_repr
    items = sorted(items)
TypeError: unorderable types: KeyClass() < KeyClass()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bug.py", line 20, in <module>
    pp.pprint(dictionary)
  File "/usr/local/lib/python3.1/pprint.py", line 106, in pprint
    self._format(object, self._stream, 0, 0, {}, 0)
  File "/usr/local/lib/python3.1/pprint.py", line 129, in _format
    rep = self._repr(object, context, level - 1)
  File "/usr/local/lib/python3.1/pprint.py", line 216, in _repr
    self._depth, level)
  File "/usr/local/lib/python3.1/pprint.py", line 228, in format
    return _safe_repr(object, context, maxlevels, level)
  File "/usr/local/lib/python3.1/pprint.py", line 277, in _safe_repr
    items = sorted(items, key=sortkey)
TypeError: unorderable types: KeyClass() < KeyClass()
msg95942 - (view) Author: David W. Lambert (LambertDW) Date: 2009-12-03 19:49
I believe this issue was resolved---expect it in a future release.
msg95953 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-12-03 22:43
Confirmed, it is fixed on all maintained branches.  So the fix will be
in 3.1.2, which should be in a month or so.
History
Date User Action Args
2022-04-11 14:56:55adminsetgithub: 51678
2009-12-03 22:43:04r.david.murraysetstatus: open -> closed
priority: normal
type: behavior

components: + Library (Lib), - None

nosy: + r.david.murray
messages: + msg95953
resolution: out of date
2009-12-03 19:49:42LambertDWsetnosy: + LambertDW
messages: + msg95942
2009-12-03 18:30:46MartinAltmayercreate