Repository navigation
Conversation
Stop building a container's one-line repr as soon as it is wider than the remaining line, instead of formatting the whole subtree at every level.
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._format()now asks_safe_repr()for a container's one-line repr with the remaining line width as a limit, and the list/tuple, dict and dict-view loops stop with a private_TooWideas soon as the repr gets wider than that, so the "does it fit?" check costs at most a line's worth of work per nesting level instead of the whole subtree. The limit is passed through an instance attribute soformat()'s signature is unchanged, and it is not applied whenformat()is overridden, since an override may shorten what it gets fromPrettyPrinter.format(); such subclasses keep the current speed.pformat()output,isreadable()andisrecursive()were compared against main on 18,000 random nested structures with various width/compact/expand/depth/sort_dicts options and are identical. A 300-deep list of 20,000 ints goes from 2.7 s to 0.11 s and ordinary wrapped output gets about 20% faster; small objects that fit on one line are a few percent slower from the extra bookkeeping.Fixes #161114
ANT-2026-1RHKXQEC
pprinton deeply nested containers #161114