8000 bpo-28850: Fix PrettyPrinter.format overrides being ignored for contents of small containers by iritkatriel · Pull Request #22120 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-28850: Fix PrettyPrinter.format overrides being ignored for contents of small containers #22120

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

Merged
merged 9 commits into from
Nov 23, 2020
Merged
Prev Previous commit
Next Next commit
Zackery's review comments (the easy ones)
  • Loading branch information
iritkatriel committed Nov 5, 2020
commit 2e1c705e79d631fc17e491427cf58563bf712205
9 changes: 6 additions & 3 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,10 @@ def _safe_repr(self, object, context, maxlevels, level):
else:
items = object.items()
for k, v in items:
krepr, kreadable, krecur = self.format(k, context, maxlevels, level)
vrepr, vreadable, vrecur = self.format(v, context, maxlevels, level)
krepr, kreadable, krecur = self.format(
k, context, maxlevels, level)
vrepr, vreadable, vrecur = self.format(
v, context, maxlevels, level)
append("%s: %s" % (krepr, vrepr))
readable = readable and kreadable and vreadable
if krecur or vrecur:
Expand Down Expand Up @@ -577,7 +579,8 @@ def _safe_repr(self, object, context, maxlevels, level):
append = components.append
level += 1
for o in object:
orepr, oreadable, orecur = self.format(o, context, maxlevels, level)
orepr, oreadable, orecur = self.format(
o, context, maxlevels, level)
append(orepr)
if not oreadable:
readable = False
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,17 @@ def test_subclassing(self):
exp = """\
{'names with spaces': 'should be presented using repr()',
others.should.not.be: like.this}"""
self.assertEqual(DottedPrettyPrinter().pformat(o), exp)

dotted_printer = DottedPrettyPrinter()
self.assertEqual(dotted_printer.pformat(o), exp)

# length(repr(obj)) < width
o1 = ['with space']
exp1 = "['with space']"
self.assertEqual(DottedPrettyPrinter().pformat(o1), exp1)
self.assertEqual(dotted_printer.pformat(o1), exp1)
o2 = ['without.space']
exp2 = "[without.space]"
self.assertEqual(DottedPrettyPrinter().pformat(o2), exp2)
self.assertEqual(dotted_printer.pformat(o2), exp2)

def test_set_reprs(self):
self.assertEqual(pprint.pformat(set()), 'set()')
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fix PrettyPrinter.format overrides being ignored for contents of small containers.
Fix :meth:`pprint.PrettyPrinter.format` overrides being ignored for contents of small containers.
0