#15009·pytest

pytest.approx loses mismatch details for same-key mappings with unequal strings

Author: torjan0Created Sep 13, 2026Updated Sep 13, 2026

pytest.approx supports non-numeric mapping values, but unequal strings under the same key cause its assertion diagnostic formatter to raise TypeError.

Save as test_approx_mapping.py:

python
import pytest


def test_mapping_string_mismatch():
    assert {"item": "a"} == pytest.approx({"item": "b"})

Run python -m pytest -q test_approx_mapping.py.

The assertion fails as expected, but its details are replaced by this message (local source path omitted):

(pytest_assertion plugin: representation of details failed: ... TypeError: unsupported operand type(s) for -: 'str' and 'str'.
 Probably an object has a faulty __repr__.)

Expected: a failed assertion with mapping mismatch details and no internal formatter exception. The equivalent unequal-string sequence retains its detail table.

#13010 and #13012 cover sequences; #13816 concerns different mapping keys. This example uses identical keys.

Reproduced on pytest 9.1.1 and development commit 3fd8675d6d798507c06cf9c60753be6d9d7b0e17, with CPython 3.12.3 on Linux 6.8.0-100-generic x86-64.

Installed packages (uv pip list --offline; this environment has no pip module):

Package          Version
---------------- -------
attrs            26.1.0
hypothesis       6.168.0
iniconfig        2.3.0
numpy            2.5.3
packaging        26.3
pluggy           1.6.0
pygments         2.21.0
pytest           9.1.1
sortedcontainers 2.4.0

GPT-6 Astra assisted; I manually reviewed this report.