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 @@ -83,6 +83,7 @@ Carlos Jenkins
Ceridwen
Charles Cloud
Charles Machalow
Charles-Meldhine Madi Mnemoi (cmnemoi)
Charnjit SiNGH (CCSJ)
Cheuk Ting Ho
Chris Mahoney
Expand Down
1 change: 1 addition & 0 deletions changelog/12444.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :func:`pytest.approx` which now correctly takes account Mapping keys order to compare them.
5 changes: 2 additions & 3 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,8 @@ def _repr_compare(self, other_side: Mapping[object, float]) -> list[str]:
max_abs_diff = -math.inf
max_rel_diff = -math.inf
different_ids = []
for (approx_key, approx_value), other_value in zip(
approx_side_as_map.items(), other_side.values(), strict=True
):
for approx_key, approx_value in approx_side_as_map.items():
other_value = other_side.get(approx_key, None)
if approx_value != other_value:
if approx_value.expected is not None and other_value is not None:
try:
Expand Down
11 changes: 11 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,17 @@ def test_strange_sequence(self):
assert b == pytest.approx(a, abs=2)
assert b != pytest.approx(a, abs=0.5)

def test_approx_on_unordered_mapping_with_mismatch(self) -> None:
"""https://github.com/pytest-dev/pytest/pull/12445"""
expected = {"a": 1, "c": 3}
actual = {"c": 5, "a": 1}

with pytest.raises(
AssertionError,
match="Mismatched elements: 1 / 2:\n Max absolute difference: 2\n",
):
assert expected == approx(actual)


class MyVec3: # incomplete
"""sequence like"""
Expand Down