Skip to content

Use pytest's pretty-printer for pformat outputs (expand-style format) - #14886

Draft
aryansk wants to merge 1 commit into
pytest-dev:mainfrom
aryansk:pformat-expanded-output-14859
Draft

Use pytest's pretty-printer for pformat outputs (expand-style format)#14886
aryansk wants to merge 1 commit into
pytest-dev:mainfrom
aryansk:pformat-expanded-output-14859

Conversation

@aryansk

@aryansk aryansk commented Aug 15, 2026

Copy link
Copy Markdown

Fixes #14859

What

Adopts the expanded pretty-printed format (the style Python 3.15 introduces via the expand option of pprint.pformat) for the diagnostic messages that pytest formats with the stdlib pretty-printer, by using pytest's own _pytest._io.pprint instead — the direction suggested by @bluetech and @Pierre-Sassoulas in the issue thread.

Adds a small pformat() convenience entry point to _pytest/_io/pprint.py (mirroring the stdlib signature, minus compact/sort_dicts/underscore_numbers) and switches three call sites to it:

  • recwarn — the "Emitted warnings:" summary in pytest.warns(...) failure messages (pformat([...], indent=2)).
  • approx — the "full mapping / full sequence" detail in the nested-approx TypeError messages.
  • saferepr.safeformat — the full local-variable listing in tracebacks (--showlocals with --tb=long, truncate_locals=False).

Because pytest's printer always expands containers, the output is now uniform across all supported Python versions — previously it would have silently changed once Python 3.15's expand option became the norm.

Why not the other pprint.pformat call sites?

The comparator summaries in _pytest/assertion/_compare_mapping.py (and the --cache-show value display) keep the stdlib printer on purpose:

  • _compare_mapping builds its "Common items"/"Left contains N more items" sub-dicts from set iteration, and the stdlib printer's key-sorting is what keeps that output deterministic. pytest's printer doesn't sort, so switching those would make assertion output depend on hash randomization.
  • --cache-show is a stable CLI display; changing its format is out of scope here.

Testing

  • New unit tests for the pformat helper in testing/io/test_pprint.py (expansion, indent, and uniformity across the versions pytest supports).
  • Updated the affected golden expectations (testing/test_recwarn.py, testing/code/test_excinfo.py) to the new uniform format.
  • Full suite: 4305 passed, 125 skipped, 12 xfailed (with --ignore=testing/plugins_integration); ruff clean on the changed files.

Adopt the expanded pretty-printed format (as introduced by pprint's
`expand` option in Python 3.15) for the warnings summary in
pytest.warns failures, approx error messages, and the full
local-variable listing in tracebacks, by formatting them with
pytest's own pretty-printer instead of the stdlib pprint.pformat.

The output is now uniform across all supported Python versions,
instead of only changing once Python 3.15 becomes the runtime.

Closes pytest-dev#14859

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Aug 15, 2026
assert full_reprlocals.lines
assert full_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
assert full_reprlocals.lines[0] == (
"l = [\n"

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.

at first glance this looks like a case that shouldnt change

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

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

use expand=True for pformat(...) in python 3.15+

2 participants