Repository navigation
Description
🤖 Drafted with Claude Code (Claude Opus 5.5): I prompted it, it reproduced the behaviour and wrote this up, I read it.
What's the problem this feature will solve?
Without -vv, the first line of an assertion explanation (assert <left> <op> <right>) has each side cut to a size computed from a fixed 80-column budget, whatever the terminal width (src/_pytest/assertion/util.py, assertrepr_compare):
# XXX: "15 chars indentation" is wrong
# ("E AssertionError: assert "); should use term width.
maxsize = (
80 - 15 - len(op) - 2
) // 2 # 15 chars indentation, 1 space around opBoth halves of that XXX show up in practice:
- Wide terminals get no benefit. On a 200-column terminal the summary is as cramped as on an 80-column one.
- The line overflows 80 columns anyway. The prefix
E AssertionError: assertis 31 characters, not 15, so for==the summary line is 31 + 30 + 4 + 30 = 95 characters.
Reproducer:
def test_x():
a = {"key_%d" % i: i for i in range(30)}
b = dict(a, key_29=0)
assert a == b$ COLUMNS=200 pytest -q test_w.py
E AssertionError: assert {'key_0': 0, ...ey_3': 3, ...} == {'key_0': 0, ...ey_3': 3, ...}
The line is 95 characters with both COLUMNS=80 and COLUMNS=200. The detailed diff below it is fine; only the summary line is affected. That summary line is often what people paste into bug reports and CI logs, and the middle-cut reprs (...ey_3') are hard to read (see also the complaints in #6682).
Describe the solution you'd like
Size the summary from the terminal width instead of 80, and count the real prefix:
maxsize = (width - len("E AssertionError: assert ") - len(op) - 2) // 2pytest_assertrepr_compare in src/_pytest/assertion/__init__.py already has config and calls config.get_terminal_writer(), so it could pass the width into util.assertrepr_compare the same way it passes highlighter and truncation_budget. Without a terminal reporter (the #14377 path), it would keep using 80.
Questions before I start on a PR:
- Is tying this line to the terminal width acceptable? It makes the output depend on the environment. Elsewhere pytest already does this (
--tboutput, the short test summary), and the test suite can pinCOLUMNS. - Should there be a lower bound, so a very narrow terminal doesn't reduce each side to a few characters? I'd keep the current 80-column size as the minimum.
- Should we cap the width on very wide terminals, or use the full width?
Alternative Solutions
-vv/verbosity_assertions = 2removes the limit entirely, but it also changes the rest of the explanation and the test-case output. You often only want the summary line to stop being cut short.- Keep 80 but fix the prefix length to 31. That fixes the overflow but not the wide-terminal case.
Additional context
- The
XXXcomment has been inassertion/util.pyfor a long time. I couldn't find an existing issue or PR about it. - Tests in
testing/test_assertion.pyandtesting/test_terminal.pymatch on the current shortened reprs. A PR would need to pin the width in those tests or update the expected output. - Happy to implement this if the approach sounds right.
Activity
surkuar commentedon Dec 4, 2026
🤖 Drafted with Claude Code (Claude Opus 5.5): I prompted it, it did the investigation and wrote this up, I read it.
While prototyping this locally I hit a limit worth knowing about before deciding on the approach.
Sizing the summary from the terminal width works as expected for long strings. For containers, it only helps up to a point. On a 200-column terminal, the reproducer from the issue becomes:
That's better than
{'key_0': 0, ...ey_3': 3, ...}, because entries are no longer cut in the middle. But it stops at 4 entries however wide the terminal is.SafeRepr(src/_pytest/_io/saferepr.py) subclassesreprlib.Reprand only overridesmaxstringand the overallmaxsize. The per-container item limits keep theirreprlibdefaults:maxdict = 4,maxlist/maxtuple/maxset= 6,maxlevel = 6. So a wider terminal fully benefits strings. A container gains width only until it reaches its item limit.I'd suggest keeping this issue to the width, and leaving the item limits alone:
SafeRepreverywhere it's used: the intermediate values shown by assertion rewriting and the reprs in tracebacks, not only this summary line.-vv, the explanation under the summary already shows which items differ, so the summary doesn't need to list more of them.If you'd rather the summary also show more items on wide terminals, one option is to scale the item limits with
maxsizefor the summary only. I think that's a separate change, and I'm happy to open a follow-up issue for it.surkuar commentedon Dec 4, 2026
🤖 Drafted with Claude Code (Claude Opus 5.5): I prompted it, it wrote this up, I read it.
I have a fix ready locally and wanted to check one decision with you before opening a PR, since it changes the output.
The question is what should happen at 80 columns, which is also the width pytest's own test suite runs at:
['a', 'b', 'c...dddddddddddd']becomes['a', 'b'...dddddddd'], and very narrow terminals leave little of each side.My branch does option 1: no existing expected output changes, and only the new tests were added. #15536 takes option 2. I'm happy to go with either, or a mix, such as option 2 with a minimum size per side. Let me know which you'd prefer and I'll open the PR to match.