Skip to content

PEP 813: keep Expand.ALWAYS through hugging, and support compact in every mode - #6

Merged
ericvsmith merged 1 commit into
ericvsmith:PEP-813-pprintfrom
warsaw:pep813-hug-always-compact
Oct 23, 2026
Merged

ericvsmith merged 1 commit into
ericvsmith:PEP-813-pprintfrom
warsaw:pep813-hug-always-compact

Conversation

@warsaw

@warsaw warsaw commented Oct 23, 2026

Copy link
Copy Markdown

Two fixes from another round of comparing the implementation with the PEP.

hug and Expand.ALWAYS. A hugged document was always wrapped in a group, so with ALWAYS a container nested in a hugging object stayed on one line: [list(range(8)), deque([[1, 2], [3]])] kept deque([[1, 2], [3]]) flat. The hugged document now drops its group below the top level, as other containers already do with ALWAYS, so every non-empty nested container expands as the PEP says.

compact in every mode. compact was used only with Expand.NEVER, and was silently ignored with FIT and ALWAYS, so Printer(compact=True) did nothing with the default expand. It now packs positional items in every mode, using PrettyPrinter's rule: an item whose one-line form fits shares lines with its packable neighbors, and any other item gets lines of its own.

[
    1, 2,
    [
        0, 1, 2, 3,
        4, 5, 6, 7,
        8, 9, 10,
        11,
    ],
    3, 4,
]

With ALWAYS, a non-empty container is never packed. Key/value and keyword items are not packed, as before.

This also fixes NEVER compact, where a broken container could start partway along a line ([1, 2, [0, 1, 2, 3,). Against PrettyPrinter(compact=True) on 3,000 random nested objects, all remaining differences are one boundary case: a packed line may now end at the last column of the width, where PrettyPrinter reserves room for a ', ' after the last item and breaks one item earlier. Our output never goes past the width where PrettyPrinter's doesn't.

Tests: test__pprint, test_pprint, test_collections, test_dataclasses, test_fstring, test_tstring and test_string pass.

🤖 Generated with Claude Code

…very mode

A hugged document was always wrapped in a group, so with Expand.ALWAYS a
container nested in a hugging object such as a deque could stay on one
line.  The hugged document now drops its group below the top level, as
other containers do with ALWAYS.

compact was used only with Expand.NEVER, and was silently ignored with
FIT and ALWAYS.  It now packs positional items in every mode, using
PrettyPrinter's rule: an item whose one-line form fits shares lines with
its packable neighbors, and any other item gets lines of its own.  With
ALWAYS, a non-empty container is never packed.  With NEVER, this also
fixes a broken container starting partway along a line, so the output
now matches PrettyPrinter's, except that a packed line can end at the
last column of the width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ericvsmith
ericvsmith merged commit 34772f4 into ericvsmith:PEP-813-pprint Oct 23, 2026
2 of 3 checks passed
@warsaw
warsaw deleted the pep813-hug-always-compact branch October 23, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants