Backtestspytestrun-20261106-234554#15172
Assertion diff doesn't pretty-print the built-in frozen dict (Python 3.15)
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the issue names: the missing `_dispatch` entry for `frozendict.__repr__` in `PrettyPrinter`, registered only when the builtin exists. The independent tests pass on both, so correctness on the issue's scenarios is equal.
Tests favor A. A adds a parametrized unit test (empty, one item, nested), a list-of-frozendicts test, a subclass-name test, an end-to-end `pytester` test that reproduces the issue's exact `-vv` Full diff output, and a stand-in Mapping test that exercises the handler via `monkeypatch` on pre-3.15 Pythons so the code path is covered everywhere CI runs. B adds a single test skipped below 3.15, wrapped in a redundant inner `if sys.version_info` with `pragma: no cover`, and no assertion-level test of the reported output.
Scope and craft also favor A. A's handler mirrors `_pprint_ordered_dict` and uses `getattr(builtins, 'frozendict', None)`, which is clean and lint-friendly. B renames `_pprint_ordered_dict` to `_pprint_dict_with_cls` (churn in an unrelated name), references the bare `frozendict` name with repeated `# noqa: F821` suppressions and `sys.version_info` guards, and extends `_safe_repr` with three new `is_frozendict` branches. That `_safe_repr` work only affects frozendicts used as dict keys or at the depth limit, which the issue does not ask about, and the fallback `repr()` already yields `frozendict({...})` on one line for the key case, so the added branching mostly adds risk and obscures the core one-line fix. B's AUTHORS addition is conventional and neutral; its `bugfix` vs A's `improvement` changelog category is a wash.
A resolves the issue with a minimal, idiomatic change and materially stronger tests, so A wins.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
4m 31s
Wall-clock time in the container
Full test suite
No new failures
292 passed · 6 skipped · 298 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
173vs 86 merged
Changed lines, ours vs merged
The issue
View on GitHubPython 3.15 added frozendict as a built-in type (PEP 814). When an assertion
comparing frozendicts fails with -vv, the "Full diff" prints each frozendict on
a single line. A dict with the same contents gets one key per line, which makes
the changed value easy to spot. With frozendict the change is buried mid-line.
def test_frozendict():
left = [frozendict(name='alice', age=30, city='Lagos'), frozendict(name='bob', age=25, city='Abuja')]
right = [frozendict(name='alice', age=30, city='Lagos'), frozendict(name='bob', age=26, city='Abuja')]
assert left == right
Output with pytest -vv:
Full diff:
[
frozendict({'name': 'alice', 'age': 30, 'city': 'Lagos'}),
- frozendict({'name': 'bob', 'age': 26, 'city': 'Abuja'}),
? ^
+ frozendict({'name': 'bob', 'age': 25, 'city': 'Abuja'}),
? ^
]
The same test using dict:
Full diff:
[
{
'name': 'alice',
'age': 30,
'city': 'Lagos',
},
{
'name': 'bob',
- 'age': 26,
? ^
+ 'age': 25,
? ^
'city': 'Abuja',
},
]
I think the cause is in src/_pytest/_io/pprint.py. PrettyPrinter._dispatch
has handlers for dict, OrderedDict, defaultdict, MappingProxyType and
others, but not frozendict, so it falls back to a one-line repr(). The
standard library's pprint in 3.15 already supports frozendict.
A possible fix is a _pprint_frozendict handler similar to _pprint_ordered_dict,
registered only when frozendict exists, so older Python versions aren't affected.
Versions
pytest 9.1.1, Python 3.15.0, Windows 11. Also seen on pytest main with Python 3.15 on Linux.
<details>
<summary>pip list</summary>
Package Version
--------- -------
colorama 0.4.6
iniconfig 2.3.1
packaging 26.3
pip 26.2.1
pluggy 1.6.0
Pygments 2.21.0
pytest 9.1.1
</details>
I'm happy to open a PR for this.
The two patches
changelog/15172.improvement.rstnew file mode 100644index 000000000..40a090176@@ -0,0 +1 @@+Assertion diffs with ``-vv`` now pretty-print the built-in ``frozendict`` (Python 3.15+, :pep:`814`) one key per line, like ``dict``.src/_pytest/_io/pprint.pyindex a550b65b0..3f50fb39d 100644@@ -23,6 +23,7 @@ # useful, thank small children who sleep at night. from __future__ import annotations +import builtins as _builtins import collections as _collections from collections.abc import Callable from collections.abc import Iterable@@ -229,6 +230,28 @@ class PrettyPrinter: _dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict + def _pprint_frozendict(+ self,+ object: Any,+ indent: int,+ allowance: int,+ context: set[int],+ level: int,+ ) -> Iterator[str]:+ if not len(object):+ yield repr(object)+ return+ cls = object.__class__+ yield cls.__name__ + "("+ yield from self._pprint_dict(object, indent, allowance, context, level)+ yield ")"++ # frozendict is a builtin since Python 3.15 (PEP 814).+ _frozendict = getattr(_builtins, "frozendict", None)+ if _frozendict is not None:+ _dispatch[_frozendict.__repr__] = _pprint_frozendict+ del _frozendict+ def _pprint_list( self, object: Any,testing/io/test_pprint.pyindex f7dbf7486..7d701122b 100644@@ -1,5 +1,6 @@ from __future__ import annotations +import builtins from collections import ChainMap from collections import Counter from collections import defaultdict@@ -8,6 +9,8 @@ from collections import OrderedDict from collections import UserDict from collections import UserList from collections import UserString+from collections.abc import Iterator+from collections.abc import Mapping from dataclasses import dataclass import textwrap from types import MappingProxyType@@ -593,6 +596,117 @@ class TestPformatLines: assert pp.pformat_lines(obj, max_lines=5) == pp.pformat(obj).splitlines() +_frozendict = getattr(builtins, "frozendict", None)+requires_frozendict = pytest.mark.skipif(+ _frozendict is None, reason="frozendict is a builtin since Python 3.15"+)+++@requires_frozendict+@pytest.mark.parametrize(+ ("data", "expected"),+ (+ pytest.param({}, "frozendict()", id="frozendict-empty"),+ pytest.param(+ {"one": 1},+ """+ frozendict({+ 'one': 1,+ })+ """,+ id="frozendict-one-item",+ ),+ pytest.param(+ {"one": 1, "two": {"three": 3}},+ """+ frozendict({+ 'one': 1,+ 'two': {+ 'three': 3,+ },+ })+ """,+ id="frozendict-nested",+ ),+ ),+)+def test_frozendict_pretty_printer(data: dict[str, Any], expected: str) -> None:+ assert _frozendict is not None+ obj = _frozendict(data)+ assert PrettyPrinter().pformat(obj) == textwrap.dedent(expected).strip()+++@requires_frozendict+def test_frozendict_in_list_one_key_per_line() -> None:+ assert _frozendict is not None+ data = [+ _frozendict(name="alice", age=30),+ _frozendict(name="bob", age=25),+ ]+ expected = """+ [+ frozendict({+ 'name': 'alice',+ 'age': 30,+ }),+ frozendict({+ 'name': 'bob',+ 'age': 25,+ }),+ ]+ """+ assert PrettyPrinter().pformat(data) == textwrap.dedent(expected).strip()+++@requires_frozendict+def test_frozendict_subclass_uses_class_name() -> None:+ assert _frozendict is not None++ MyFrozenDict = type("MyFrozenDict", (_frozendict,), {})+ pp = PrettyPrinter()+ assert pp.pformat(MyFrozenDict(one=1)) == "MyFrozenDict({\n 'one': 1,\n})"+++class _StandInFrozenDict(Mapping[str, Any]):+ """Mimics the builtin ``frozendict`` so its handler is exercised on+ Python versions that predate it."""++ def __init__(self, **kwargs: Any) -> None:+ self._data = dict(kwargs)++ def __getitem__(self, key: str) -> Any:+ return self._data[key]++ def __iter__(self) -> Iterator[str]:+ return iter(self._data)++ def __len__(self) -> int:+ return len(self._data)++ def __repr__(self) -> str:+ if not self._data:+ return "_StandInFrozenDict()"+ return f"_StandInFrozenDict({self._data!r})"+++def test_pprint_frozendict_handler(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setitem(+ PrettyPrinter._dispatch,+ _StandInFrozenDict.__repr__,+ PrettyPrinter._pprint_frozendict,+ )+ pp = PrettyPrinter()+ assert pp.pformat(_StandInFrozenDict()) == "_StandInFrozenDict()"+ assert pp.pformat(_StandInFrozenDict(one=1, two=2)) == textwrap.dedent(+ """+ _StandInFrozenDict({+ 'one': 1,+ 'two': 2,+ })+ """+ ).strip()++ def test_pformat_sorts_heterogeneous_set() -> None: # The set sort tries a natural sort first and falls back to a key # that compares the element types' names only for unorderabletesting/test_assertion.pyindex 035aad31c..ad7a3ae02 100644@@ -1,6 +1,7 @@ # mypy: allow-untyped-defs from __future__ import annotations +import builtins from collections import ChainMap from collections import UserDict from collections.abc import Iterator@@ -762,6 +763,40 @@ class TestAssert_reprcompare: "Use -v to get more diff", ] + @pytest.mark.skipif(+ not hasattr(builtins, "frozendict"),+ reason="frozendict is a builtin since 3.15",+ )+ def test_frozendict_full_diff_one_key_per_line(self, pytester: Pytester) -> None:+ pytester.makepyfile(+ """+ def test_frozendict():+ alice = frozendict(name='alice', age=30)+ left = [alice, frozendict(name='bob', age=25)]+ right = [alice, frozendict(name='bob', age=26)]+ assert left == right+ """+ )+ result = pytester.runpytest("-vv")+ result.stdout.fnmatch_lines(+ [+ "E Full diff: (-: missing in left side, +: extra in left side)",+ "E [",+ "E frozendict({",+ "E 'name': 'alice',",+ "E 'age': 30,",+ "E }),",+ "E frozendict({",+ "E 'name': 'bob',",+ "E - 'age': 26,",+ "E ? ^",+ "E + 'age': 25,",+ "E ? ^",+ "E }),",+ "E ]",+ ]+ )+ def test_iterable_full_diff_ci( self, monkeypatch: MonkeyPatch, pytester: Pytester ) -> None:
AUTHORSindex 1d1af0015..9e4327c77 100644@@ -499,6 +499,7 @@ Tim Strazny TJ Bruno Tobias Diez Tobias Petersen+Toluwalase Akinyemi Tom Dalton Tom Most Tom Vinerchangelog/15172.bugfix.rstnew file mode 100644index 000000000..14a2033b5@@ -0,0 +1 @@+Assertion diffs now pretty-print the built-in :class:`frozendict` (Python 3.15+) across multiple lines, similarly to how :class:`dict` is displayed.src/_pytest/_io/pprint.pyindex a550b65b0..1f443e9d7 100644@@ -29,6 +29,7 @@ from collections.abc import Iterable from collections.abc import Iterator import dataclasses as _dataclasses import re+import sys import types as _types from typing import Any @@ -211,7 +212,7 @@ class PrettyPrinter: _dispatch[dict.__repr__] = _pprint_dict - def _pprint_ordered_dict(+ def _pprint_dict_with_cls( self, object: Any, indent: int,@@ -227,7 +228,9 @@ class PrettyPrinter: yield from self._pprint_dict(object, indent, allowance, context, level) yield ")" - _dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict+ _dispatch[_collections.OrderedDict.__repr__] = _pprint_dict_with_cls+ if sys.version_info >= (3, 15):+ _dispatch[frozendict.__repr__] = _pprint_dict_with_cls # noqa: F821 def _pprint_list( self,@@ -621,12 +624,26 @@ class PrettyPrinter: r = getattr(typ, "__repr__", None) - if issubclass(typ, dict) and r is dict.__repr__:+ if sys.version_info >= (3, 15):+ is_frozendict = (+ issubclass(typ, frozendict) # noqa: F821+ and r is frozendict.__repr__ # noqa: F821+ )+ else:+ is_frozendict = False++ if (issubclass(typ, dict) and r is dict.__repr__) or is_frozendict: if not object:- return "{}"+ if is_frozendict:+ return f"{typ.__name__}()"+ else:+ return "{}" objid = id(object) if maxlevels and level >= maxlevels:- return "{...}"+ if is_frozendict:+ return f"{typ.__name__}({{...}})"+ else:+ return "{...}" if objid in context: return _recursion(object) context.add(objid)@@ -638,7 +655,11 @@ class PrettyPrinter: vrepr = self._safe_repr(v, context, maxlevels, level) append(f"{krepr}: {vrepr}") context.remove(objid)- return "{{{}}}".format(", ".join(components))+ rep = "{{{}}}".format(", ".join(components))+ if is_frozendict:+ return f"{typ.__name__}({rep})"+ else:+ return rep if (issubclass(typ, list) and r is list.__repr__) or ( issubclass(typ, tuple) and r is tuple.__repr__testing/io/test_pprint.pyindex f7dbf7486..f4ad26996 100644@@ -9,6 +9,7 @@ from collections import UserDict from collections import UserList from collections import UserString from dataclasses import dataclass+import sys import textwrap from types import MappingProxyType from types import SimpleNamespace@@ -651,6 +652,56 @@ def test_simplenamespace_subclass_uses_class_name() -> None: assert pp.pformat(MyNamespace(one=1)) == "MyNamespace(\n one=1,\n)" +@pytest.mark.skipif(+ sys.version_info < (3, 15), reason="frozendict is a builtin since Python 3.15"+)+def test_frozendict() -> None:+ # For mypy, which checks against Python 3.10.+ if sys.version_info >= (3, 15): # pragma: no cover+ pp = PrettyPrinter()+ assert pp.pformat(frozendict()) == "frozendict()" # noqa: F821+ assert pp.pformat(frozendict({"one": 1, "two": 2})) == textwrap.dedent( # noqa: F821+ """\+ frozendict({+ 'one': 1,+ 'two': 2,+ })"""+ )++ class MyFrozenDict(frozendict): # noqa: F821+ pass++ assert pp.pformat(MyFrozenDict({"one": 1})) == textwrap.dedent(+ """\+ MyFrozenDict({+ 'one': 1,+ })"""+ )++ # As a dict key (frozendict is hashable), it goes through _safe_repr.+ key = frozendict({"one": 1}) # noqa: F821+ assert pp.pformat({key: 0}) == textwrap.dedent(+ """\+ {+ frozendict({'one': 1}): 0,+ }"""+ )+ nested = frozendict({"one": frozendict({"two": 2})}) # noqa: F821+ assert PrettyPrinter(depth=1).pformat({nested: 0}) == textwrap.dedent(+ """\+ {+ frozendict({...}): 0,+ }"""+ )+ empty = frozendict() # noqa: F821+ assert pp.pformat({empty: 0}) == textwrap.dedent(+ """\+ {+ frozendict(): 0,+ }"""+ )++ def test_safe_tuple_sorts_unorderable_pairs() -> None: # ``_safe_tuple`` wraps each element of a 2-tuple in ``_safe_key`` so a # list of pairs with unorderable elements can be sorted without raising.