gh-89774: Fix pprint ignoring the depth limit at small width#153157
Open
tonghuaroot wants to merge 1 commit into
Open
gh-89774: Fix pprint ignoring the depth limit at small width#153157tonghuaroot wants to merge 1 commit into
tonghuaroot wants to merge 1 commit into
Conversation
Skip the per-type re-dispatch in _format once the depth limit is reached, so depth truncation no longer depends on width.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PrettyPrinter._formatre-dispatches to the per-type formatter (_pprint_dict,_pprint_list, ...) whenever a node's repr exceeds the available line width. Those formatters re-expand children that_safe_reprhad already folded to...at the depth limit, so a smallwidthsilently defeatsdepth.The fix skips the re-dispatch once the depth limit is reached, using the same boundary
_safe_repruses (level >= self._depth). Depth truncation is now independent of width, which also restores the narrow-width example documented inDoc/library/pprint.rst.The regression test covers the dict, list, and tuple formatter branches and asserts that a narrow width folds at the same level as the default width.
depthis always positive (__init__rejectsdepth <= 0), so theis not Noneguard and_safe_repr's truthiness check are equivalent here.I have left the backport labels for a maintainer to assign.