Skip to content

gh-155596: Fix pprint expand mode ignoring width for nested values - #155926

Open
cobed95 wants to merge 2 commits into
python:mainfrom
cobed95:gh-155596
Open

gh-155596: Fix pprint expand mode ignoring width for nested values#155926
cobed95 wants to merge 2 commits into
python:mainfrom
cobed95:gh-155596

Conversation

@cobed95

@cobed95 cobed95 commented Aug 17, 2026

Copy link
Copy Markdown

Issue

Summary

In the new "expand" mode, 'key': prefix needs to be accounted for.
Here we add a new argument prefix_len to _format, and

  1. In "expand" mode, subtract it from max_width so that width is respected.
  2. In "aligned" mode, fold it into indent so 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=40 setting, so I would like to regard
these updates not as regressions, but rather as corrections.

@python-cla-bot

python-cla-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@cobed95

cobed95 commented Aug 17, 2026

Copy link
Copy Markdown
Author

@hugovk @corona10 Hi sprint mentors, I would like to request reviews from you :)
Thanks a lot.

@github-project-automation github-project-automation Bot moved this to Todo in Sprint Aug 17, 2026
@hugovk hugovk added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 29, 2026
Comment thread Lib/pprint.py Outdated
Comment on lines +189 to +190
def _format(self, object, stream, indent, allowance, context, level,
prefix_len=0):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.15 pre-release feature fixes, bugs and security fixes sprint

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants