gh-155596: Fix pprint expand mode ignoring width for nested values - #155926
gh-155596: Fix pprint expand mode ignoring width for nested values#155926cobed95 wants to merge 2 commits into
Conversation
| def _format(self, object, stream, indent, allowance, context, level, | ||
| prefix_len=0): |
There was a problem hiding this comment.
_format is "private", but we should be careful that PrettyPrinter can still be subclassed by others who won't have prefix_len, so we'd want to avoid:
TypeError: PrettyPrinter._format() got an unexpected keyword argument 'prefix_len'We've run into issues like this before. Is there a way to fix this without adding prefix_len here?
There was a problem hiding this comment.
@hugovk You're right, this would break subclasses.
I've reworked it to hand the prefix width over out of band instead. _format() keeps its existing signature and reads self._pending_prefix_len, which a new _format_child() sets immediately before the call and clears in a finally. The four call sites go through that helper, so _child_indent() is used as before and the diff gets smaller too.
A subclass that overrides _format() then just doesn't get the width correction.
One thing I wasn't sure about: I made _pending_prefix_len a class attribute so a subclass that skips super().__init__() still has a default. Happy to move it into __init__ if you'd prefer it explicit.
…ix width over on the instance instead
Issue
pprint(..., expand=True)'s "best effort" for adhering towidth=...seems not very "best" #155596Summary
In the new "expand" mode,
'key':prefix needs to be accounted for.Here we add a new argument
prefix_lento_format, andmax_widthso that width is respected.indentso that existing behaviour is preserved.Tests
One new test was added and two existing tests' expectations have been updated.
Existing expectations were violating the
width=40setting, so I would like to regardthese updates not as regressions, but rather as corrections.
pprint(..., expand=True)'s "best effort" for adhering towidth=...seems not very "best" #155596