Skip to content

Assertion summary line ignores terminal width (and overflows 80 columns) #15531

Backlog

Description

@surkuar

🤖 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 op

Both halves of that XXX show up in practice:

  1. Wide terminals get no benefit. On a 200-column terminal the summary is as cramped as on an 80-column one.
  2. The line overflows 80 columns anyway. The prefix E AssertionError: assert is 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) // 2

pytest_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 (--tb output, the short test summary), and the test suite can pin COLUMNS.
  • 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 = 2 removes 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 XXX comment has been in assertion/util.py for a long time. I couldn't find an existing issue or PR about it.
  • Tests in testing/test_assertion.py and testing/test_terminal.py match 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

  1. surkuar commented on Dec 4, 2026

    @surkuar
    ContributorAuthor

    🤖 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:

    E       AssertionError: assert {'key_0': 0, 'key_1': 1, 'key_2': 2, 'key_3': 3, ...} == {'key_0': 0, 'key_1': 1, 'key_2': 2, 'key_3': 3, ...}
    

    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) subclasses reprlib.Repr and only overrides maxstring and the overall maxsize. The per-container item limits keep their reprlib defaults: 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:

    • Raising them changes SafeRepr everywhere it's used: the intermediate values shown by assertion rewriting and the reprs in tracebacks, not only this summary line.
    • Below -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 maxsize for the summary only. I think that's a separate change, and I'm happy to open a follow-up issue for it.

  2. surkuar commented on Dec 4, 2026

    @surkuar
    ContributorAuthor

    🤖 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:

    1. Keep the current output. Use the terminal width, but never shorten below today's size. At 80 columns nothing changes. Only terminals wider than about 96 columns show more. A summary line can still overflow an 80-column terminal, as it does today.
    2. Fit the line exactly. Size the summary from the real 31-character prefix with no minimum. Nothing overflows any more, but the default output gets shorter, e.g. ['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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions