Skip to content

Commit 5d5b40d

Browse files
committed
gh-141510: Support frozendict in pprint
1 parent 6ef2578 commit 5d5b40d

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

Lib/pprint.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,14 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
220220

221221
def _pprint_dict(self, object, stream, indent, allowance, context, level):
222222
write = stream.write
223-
write('{')
223+
typ = object.__class__
224+
if typ is frozendict:
225+
stream.write(typ.__name__ + '({')
226+
end = '})'
227+
indent += len(typ.__name__) + 1
228+
else:
229+
write('{')
230+
end = '}'
224231
if self._indent_per_level > 1:
225232
write((self._indent_per_level - 1) * ' ')
226233
length = len(object)
@@ -231,9 +238,10 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
231238
items = object.items()
232239
self._format_dict_items(items, stream, indent, allowance + 1,
233240
context, level)
234-
write('}')
241+
write(end)
235242

236243
_dispatch[dict.__repr__] = _pprint_dict
244+
_dispatch[frozendict.__repr__] = _pprint_dict
237245

238246
def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level):
239247
if not len(object):

Lib/test/test_pprint.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ def test_basic_line_wrap(self):
330330
for type in [dict, dict2]:
331331
self.assertEqual(pprint.pformat(type(o)), exp)
332332

333+
exp = """\
334+
frozendict({'RPM_cal': 0,
335+
'RPM_cal2': 48059,
336+
'Speed_cal': 0,
337+
'controldesk_runtime_us': 0,
338+
'main_code_runtime_us': 0,
339+
'read_io_runtime_us': 0,
340+
'write_io_runtime_us': 43690})"""
341+
self.assertEqual(pprint.pformat(frozendict(o)), exp)
342+
333343
o = range(100)
334344
exp = 'dict_keys([%s])' % ',\n '.join(map(str, o))
335345
keys = dict.fromkeys(o).keys()

0 commit comments

Comments
 (0)