Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ Tim Strazny
TJ Bruno
Tobias Diez
Tobias Petersen
Toluwalase Akinyemi
Tom Dalton
Tom Most
Tom Viner
Expand Down
1 change: 1 addition & 0 deletions changelog/15172.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Assertion diffs now pretty-print the built-in :class:`frozendict` (Python 3.15+) across multiple lines, the same way :class:`dict` is displayed.
5 changes: 5 additions & 0 deletions src/_pytest/_io/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from collections.abc import Iterator
import dataclasses as _dataclasses
import re
import sys
import types as _types
from typing import Any

Expand Down Expand Up @@ -229,6 +230,10 @@ def _pprint_ordered_dict(

_dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict

if sys.version_info >= (3, 15):
# frozendict's repr has the same shape as OrderedDict's: Name({...})
_dispatch[frozendict.__repr__] = _pprint_ordered_dict # noqa: F821

def _pprint_list(
self,
object: Any,
Expand Down
27 changes: 27 additions & 0 deletions testing/io/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from collections import UserList
from collections import UserString
from dataclasses import dataclass
import sys
import textwrap
from types import MappingProxyType
from types import SimpleNamespace
Expand Down Expand Up @@ -651,6 +652,32 @@ class MyNamespace(SimpleNamespace):
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:
if sys.version_info >= (3, 15): # for mypy, which checks against Python 3.10
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,
})"""
)


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.
Expand Down
Loading