From f9881afdbdcc540d23658bfae11b2610eb126211 Mon Sep 17 00:00:00 2001 From: Charles-Meldhine Madi Mnemoi Date: Wed, 15 Oct 2025 14:51:36 +0200 Subject: [PATCH 1/6] fix(testing): `pytest.approx` returns a clearer error mesage when comparing mappings with different keys --- testing/python/approx.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/testing/python/approx.py b/testing/python/approx.py index f870b9bd4d8..5cc7dc48727 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -1,21 +1,18 @@ # mypy: allow-untyped-defs from __future__ import annotations -from contextlib import contextmanager import decimal -from decimal import Decimal -from fractions import Fraction -from math import inf -from math import nan -from math import sqrt import operator -from operator import eq -from operator import ne import re +from contextlib import contextmanager +from decimal import Decimal +from fractions import Fraction +from math import inf, nan, sqrt +from operator import eq, ne +import pytest from _pytest.pytester import Pytester from _pytest.python_api import _recursive_sequence_map -import pytest from pytest import approx From 3e1a5a760bf0f6072b9b6ef3d319ef42b2053f40 Mon Sep 17 00:00:00 2001 From: Charles-Meldhine Madi Mnemoi Date: Wed, 15 Oct 2025 14:54:55 +0200 Subject: [PATCH 2/6] docs: add changelog entry --- changelog/13816.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/13816.bugfix.rst diff --git a/changelog/13816.bugfix.rst b/changelog/13816.bugfix.rst new file mode 100644 index 00000000000..432fc519ed5 --- /dev/null +++ b/changelog/13816.bugfix.rst @@ -0,0 +1 @@ +Fixed :func:`pytest.approx` which now returns a clearer error message when comparing mappings with different keys. From 06c9d7d62a0f4d43c5acb4c5991d0b5cfec25392 Mon Sep 17 00:00:00 2001 From: Charles-Meldhine Madi Mnemoi Date: Wed, 15 Oct 2025 13:02:39 +0200 Subject: [PATCH 3/6] fix(testing): `pytest.approx` correctly take account Mapping keys order to compare them --- src/_pytest/python_api.py | 5 ++--- testing/python/approx.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 1e389eb0663..e9dfdc857e2 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -256,9 +256,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: diff --git a/testing/python/approx.py b/testing/python/approx.py index 5cc7dc48727..e02c4704dad 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -1059,6 +1059,17 @@ def test_approx_dicts_with_mismatch_on_keys(self) -> None: ): assert actual == approx(expected) + 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""" From 99d108baed65c80eeca08547423758700df4c6d0 Mon Sep 17 00:00:00 2001 From: Charles-Meldhine Madi Mnemoi Date: Wed, 15 Oct 2025 13:06:44 +0200 Subject: [PATCH 4/6] doc(changelog): add entry for fixing `pytest.approx` to account for Mapping keys order --- changelog/12444.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/12444.bugfix.rst diff --git a/changelog/12444.bugfix.rst b/changelog/12444.bugfix.rst new file mode 100644 index 00000000000..146cfc7ab24 --- /dev/null +++ b/changelog/12444.bugfix.rst @@ -0,0 +1 @@ +Fixed :func:`pytest.approx` which now correctly takes account Mapping keys order to compare them. From 8b50d4145bc0aebce3a4aeebdb4c1b91dc980eef Mon Sep 17 00:00:00 2001 From: Charles-Meldhine Madi Mnemoi Date: Tue, 2 Dec 2025 15:39:40 +0100 Subject: [PATCH 5/6] remove deprecated changelog entry --- changelog/13816.bugfix.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 changelog/13816.bugfix.rst diff --git a/changelog/13816.bugfix.rst b/changelog/13816.bugfix.rst deleted file mode 100644 index 432fc519ed5..00000000000 --- a/changelog/13816.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fixed :func:`pytest.approx` which now returns a clearer error message when comparing mappings with different keys. From d6e1bce433f571ad92057e03e35aea6d5172be89 Mon Sep 17 00:00:00 2001 From: Charles-Meldhine Madi Mnemoi Date: Tue, 2 Dec 2025 15:48:40 +0100 Subject: [PATCH 6/6] apply ruff fixes --- testing/python/approx.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/testing/python/approx.py b/testing/python/approx.py index e02c4704dad..1e59e61c43a 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -1,18 +1,21 @@ # mypy: allow-untyped-defs from __future__ import annotations -import decimal -import operator -import re from contextlib import contextmanager +import decimal from decimal import Decimal from fractions import Fraction -from math import inf, nan, sqrt -from operator import eq, ne +from math import inf +from math import nan +from math import sqrt +import operator +from operator import eq +from operator import ne +import re -import pytest from _pytest.pytester import Pytester from _pytest.python_api import _recursive_sequence_map +import pytest from pytest import approx