Closed 7de15a01-df33-4df0-b6b5-81e09b8a0a1b closed 14 years ago
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()
I believe this issue was resolved---expect it in a future release.
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.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at =
created_at =
labels = ['type-bug', 'library']
title = 'PrettyPrinter cannot print dicts with unsortable keys'
updated_at =
user = 'https://github.com/MartinAltmayer'
```
bugs.python.org fields:
```python
activity =
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date =
closer = 'r.david.murray'
components = ['Library (Lib)']
creation =
creator = 'MartinAltmayer'
dependencies = []
files = []
hgrepos = []
issue_num = 7429
keywords = []
message_count = 3.0
messages = ['95939', '95942', '95953']
nosy_count = 3.0
nosy_names = ['LambertDW', 'r.david.murray', 'MartinAltmayer']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7429'
versions = ['Python 3.1']
```