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: pprint doesn't know how to print a defaultdict
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: general pprint rewrite
View: 7434
Assigned To: Nosy List: amaury.forgeotdarc, bpb, eric.araujo, fdrake, giampaolo.rodola, jackdied, ncoghlan, ncw, rbp, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2009-02-02 16:34 by ncw, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue_5131.patch jackdied, 2009-03-26 21:09 patch, test, NEWS, ACKS
Messages (15)
msg80948 - (view) Author: Nick Craig-Wood (ncw) * Date: 2009-02-02 16:33
I noticed this the other day when debugging a program that neither set()
nor defaultdict() pprint() properly

Same under 3.1 and 2.5 (Not tried 2.6/2.7 but I assume it is the same)

>>> pprint(set(range(100)))
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99])

>>> from collections import defaultdict
>>> pprint(defaultdict(int).fromkeys(range(100)))
defaultdict(None, {0: None, 1: None, 2: None, 3: None, 4: None, 5: None,
6: None, 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13:
None, 14: None, 15: None, 16: None, 17: None, 18: None, 19: None, 20:
None, 21: None, 22: None, 23: None, 24: None, 25: None, 26: None, 27:
None, 28: None, 29: None, 30: None, 31: None, 32: None, 33: None, 34:
None, 35: None, 36: None, 37: None, 38: None, 39: None, 40: None, 41:
None, 42: None, 43: None, 44: None, 45: None, 46: None, 47: None, 48:
None, 49: None, 50: None, 51: None, 52: None, 53: None, 54: None, 55:
None, 56: None, 57: None, 58: None, 59: None, 60: None, 61: None, 62:
None, 63: None, 64: None, 65: None, 66: None, 67: None, 68: None, 69:
None, 70: None, 71: None, 72: None, 73: None, 74: None, 75: None, 76:
None, 77: None, 78: None, 79: None, 80: None, 81: None, 82: None, 83:
None, 84: None, 85: None, 86: None, 87: None, 88: None, 89: None, 90:
None, 91: None, 92: None, 93: None, 94: None, 95: None, 96: None, 97:
None, 98: None, 99: None})
>>>
msg84221 - (view) Author: Jack Diederich (jackdied) * (Python committer) Date: 2009-03-26 21:09
sets and frozensets have already been updated to format like lists. 
This patch formats defaultdicts like dicts.
msg84533 - (view) Author: Nick Craig-Wood (ncw) * Date: 2009-03-30 10:27
I couldn't actually get this patch to apply to the py3k branch :-(

$ patch -p0 --dry-run < issue_5131.patch
patching file Misc/NEWS
Hunk #1 FAILED at 2598.
1 out of 1 hunk FAILED -- saving rejects to file Misc/NEWS.rej
patching file Misc/ACKS
Hunk #1 succeeded at 147 (offset -1 lines).
Hunk #2 succeeded at 791 (offset 3 lines).
patching file Lib/pprint.py
Hunk #1 FAILED at 37.
Hunk #2 FAILED at 137.
Hunk #3 FAILED at 168.
3 out of 3 hunks FAILED -- saving rejects to file Lib/pprint.py.rej
patching file Lib/test/test_pprint.py
Hunk #1 succeeded at 408 (offset -6 lines).

$ svn info
Path: .
URL: http://svn.python.org/projects/python/branches/py3k
Repository Root: http://svn.python.org/projects
Repository UUID: 6015fed2-1504-0410-9fe1-9d1591cc4771
Revision: 70705
Node Kind: directory
Schedule: normal
Last Changed Author: antoine.pitrou
Last Changed Rev: 70696
Last Changed Date: 2009-03-29 20:30:55 +0100 (Sun, 29 Mar 2009)
msg84572 - (view) Author: Jack Diederich (jackdied) * (Python committer) Date: 2009-03-30 16:07
py3k is different enough (esp the NEWS) that I'll have to apply it by
hand.  This patch was against the 2.x trunk.
msg84758 - (view) Author: Nick Craig-Wood (ncw) * Date: 2009-03-31 07:42
Oops, my bad, I assumed the patch would by for py3k!

I applied it to trunk and tested it. It works very well - thank you for
fixing that :-)
msg109670 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-07-09 02:54
I removed 'a set or' from the title since sets are no longer an issue.

Since .fromkeys is a class method, it should be called on a class rather than an instance. I was initially fooled by the irrelevant addition of '(int)'. 100 is unnecessarily large for example and review.

Patch changes code and test. However, I cannot tell what effect is has merely from reading the patch. Please add an example of the altered output, with 100 lowered to 5 or 10.

A doc change is not needed since it merely says "The pprint module provides a capability to “pretty-print” arbitrary Python data structures "

I suspect the patch needs some update to apply to for 3.2a. Has Tarek's name been fixed already?
msg111157 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-07-22 09:11
Patch looks good to me.
(Some day the pprint module should grow some way to register repr for user types, so we can stop adding special cases; but defaultdict() is a builtin)
msg114920 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-25 15:24
Jack, can you update your patch to reflect Terry’s review?

Amaury, have you opened a feature request about your registration idea?
msg114921 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-25 15:26
BTW, Tarek’s name is valid UTF-8 in py3k.
msg117527 - (view) Author: Ben Bass (bpb) Date: 2010-09-28 14:50
Same applies to collections.deque, which seems closely related (being another collections class).  Can this get addressed here or should I open another issue?

(just been pprinting defaultdict(deque) objects, which clearly fails :)
msg117565 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-29 00:14
Ben, I don't think there is any value is opening more issues like pprint-doesn't-handle-object-x (named tuples, defautdicts, deques, generators, etc).

As it is currently designed, pprint doesn't offer usable hooks and it is not easy to build-out to handle new object types.  For the most part, users just cast to a list before calling pprint.

ISTM, the RightAnswer(tm) is to overhaul pprint and add hooks for handling new objects.  Ideally, there would be flexible print options and reprlib-like features for limiting output and for marking recursive constructs with "..."

It would be better to start fresh than to continually pile on to the current hodge-podge.
msg117591 - (view) Author: Nick Craig-Wood (ncw) * Date: 2010-09-29 10:32
Raymond Hettinger (rhettinger) wrote:
> Ben, I don't think there is any value is opening more issues like pprint-doesn't-handle-object-x (named tuples, defautdicts, deques, generators, etc).
> 
> As it is currently designed, pprint doesn't offer usable hooks and it is not easy to build-out to handle new object types.  For the most part, users just cast to a list before calling pprint.

I mildly disagree, IMHO pprint should be able to make a decent job of all the built in types otherwise it loses its value as a debugging tool.  It is a suprise when a built in type doesn't pprint properly.

This would surely be an excellent use of the abstract base classes defined in the collections module for pprint to make a best guess as to how to print types it doesn't understand directly?

> ISTM, the RightAnswer(tm) is to overhaul pprint and add hooks for handling new objects.  Ideally, there would be flexible print options and reprlib-like features for limiting output and for marking recursive constructs with "..."

That is a very good idea, but might be unecessary with the ABC idea above
msg117632 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-09-29 17:30
>IMHO pprint should be able to make a decent job of all the built in types 

Agreed, already true as far as I know, and irrelevant. This issue is not about built-in types in the builtins module, as documented Lib Ref chapter 5 *Built-in Types*. Collections is an Python-coded stdlib  module that happens to import a couple of its classes from _collections, written in C for speed. 

I agree with Raymond that pprint needs a generic solution better than special-casing one class after another. Your idea of using ABCs intriguing.
msg117781 - (view) Author: Nick Craig-Wood (ncw) * Date: 2010-10-01 08:59
Terry J. Reedy (terry.reedy) wrote:
> > IMHO pprint should be able to make a decent job of all the built in types 
>
> Agreed, already true as far as I know, and irrelevant. This issue is not about built-in types in the builtins module, as documented Lib Ref chapter 5 *Built-in Types*. Collections is an Python-coded stdlib  module that happens to import a couple of its classes from _collections, written in C for speed. 

My bad - not precise enough!  I meant all the stdlib types rather than the builtin types.
msg207286 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-01-04 14:43
Closing this as a duplicate of issue 7434 (which is about redesigning pprint to make it easier to add support for new types)
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49381
2014-01-04 14:43:42ncoghlansetstatus: open -> closed

superseder: general pprint rewrite

nosy: + ncoghlan
messages: + msg207286
resolution: accepted -> duplicate
stage: patch review -> resolved
2011-12-15 17:20:10giampaolo.rodolasetnosy: + giampaolo.rodola
2010-11-08 22:33:25rbpsetnosy: + rbp
2010-10-01 08:59:04ncwsetmessages: + msg117781
2010-09-29 17:30:52terry.reedysetmessages: + msg117632
2010-09-29 10:32:15ncwsetmessages: + msg117591
2010-09-29 00:14:36rhettingersetnosy: + rhettinger
messages: + msg117565
2010-09-28 14:50:25bpbsetnosy: + bpb
messages: + msg117527
2010-08-25 15:26:45eric.araujosetmessages: + msg114921
2010-08-25 15:24:53eric.araujosetnosy: + eric.araujo
messages: + msg114920
2010-07-22 09:11:13amaury.forgeotdarcsetresolution: accepted

messages: + msg111157
nosy: + amaury.forgeotdarc
2010-07-09 04:34:43rhettingersetnosy: + fdrake
2010-07-09 02:54:47terry.reedysetnosy: + terry.reedy
title: pprint doesn't know how to print a set or a defaultdict -> pprint doesn't know how to print a defaultdict
messages: + msg109670

versions: + Python 3.2, - Python 2.5, Python 3.1
stage: patch review
2009-03-31 07:42:22ncwsetmessages: + msg84758
2009-03-30 16:07:12jackdiedsetmessages: + msg84572
2009-03-30 10:27:49ncwsetmessages: + msg84533
2009-03-26 21:09:29jackdiedsetfiles: + issue_5131.patch

nosy: + jackdied
messages: + msg84221

keywords: + patch
2009-02-02 16:34:00ncwcreate