New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pprint.pprint raises TypeError on dictionaries with user-defined types as keys #54226
Comments
The pprint function in the python 3.1 pprint module fails when This seems related to bpo-3976 but it was fixed in r76389 and In Python 2.6, the following works fine: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pprint import pprint
>>> class A(object): pass
...
>>> pprint({A:1, 1:2})
{1: 2, <class __main__.A at 0xb77dc47c>: 1} But in Python 3.1, it fails with the error below: Python 3.1.2 (r312:79147, Apr 15 2010, 12:35:07)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pprint import pprint
>>> class A: pass
...
>>> pprint({A:1, 1:2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.1/pprint.py", line 55, in pprint
printer.pprint(object)
File "/usr/lib/python3.1/pprint.py", line 132, in pprint
self._format(object, self._stream, 0, 0, {}, 0)
File "/usr/lib/python3.1/pprint.py", line 155, in _format
rep = self._repr(object, context, level - 1)
File "/usr/lib/python3.1/pprint.py", line 242, in _repr
self._depth, level)
File "/usr/lib/python3.1/pprint.py", line 254, in format
return _safe_repr(object, context, maxlevels, level)
File "/usr/lib/python3.1/pprint.py", line 296, in _safe_repr
items = sorted(object.items(), key=_safe_tuple)
File "/usr/lib/python3.1/pprint.py", line 89, in __lt__
rv = self.obj.__lt__(other.obj)
TypeError: expected 1 arguments, got 0 |
FWIW, the problem still occurs on the most recent release31-maint checkout (as of r85323), and does not happen on py3k (3.2a2). |
If I'm understanding this correctly, this fails on 3.1 and not (although, actually, it does) on py3k/3.2 because:
>>> pprint.pprint({A(): 1, 1: 2})
{<__main__.A object at 0x8594d4c>: 1, 1: 2}
Anyway, py3k still fails when you force the comparison to happen on the class: >>> class B(object): pass
...
>>> pprint.pprint({A: 1, B: 2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 55, in pprint
printer.pprint(object)
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 132, in pprint
self._format(object, self._stream, 0, 0, {}, 0)
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 155, in _format
rep = self._repr(object, context, level - 1)
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 245, in _repr
self._depth, level)
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 257, in format
return _safe_repr(object, context, maxlevels, level)
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 299, in _safe_repr
items = sorted(object.items(), key=_safe_tuple)
File "/home/rbp/python/dev/py3k/Lib/pprint.py", line 89, in __lt__
rv = self.obj.__lt__(other.obj)
TypeError: expected 1 arguments, got 0
>>> So, basically, the fix on bpo-3976 does't (always) work when there are classes as dict keys. I think switching from rv = self.obj.__lt__(other.obj)
if rv is NotImplemented: to something like try: solves this. Or we can check first whether self.obj is a 'type', but I think it gets convoluted. If anyone can confirm that that's the way to go, I can produce one (though it's a trivial one). Raymond? |
A simpler change would to replace: |
Confirmed on python 3.2 and 3.3. |
Here's a patch with fix and tests. Note that class objects are not comparable and have the same type so they |
New changeset 03cda5360dc6 by Florent Xicluna in branch '3.2': New changeset 4d0dcfbdf45b by Florent Xicluna in branch 'default': |
Thanks for this patch. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: