You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pytest.approx loses mismatch details for same-key mappings with unequal strings #15009
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):
I'd like to take this. Reproduced on main, and the cause is a small asymmetry between the mapping and sequence formatters.
ApproxMapping._repr_compare (src/_pytest/approx.py) guards the diff arithmetic with:
exceptZeroDivisionError:
pass
while ApproxSequenceLike._repr_compare guards the equivalent block with except TypeError, added in #13012 for #13010. Subtracting two strings raises TypeError, so the mapping path lets it escape into the assertion-repr hook, which is why the details are replaced by the "representation of details failed" message while the sequence equivalent still prints its table:
# mapping -> TypeError: unsupported operand type(s) for -: 'str' and 'str'
# sequence -> Index | Obtained | Expected
# 1 | a | b
Widen the guard to except (ZeroDivisionError, TypeError) so non-numeric values are skipped for the max-abs/max-rel calculations but still reported in the mismatch table.
Add a mapping test in testing/python/approx.py alongside test_mixed_sequence, covering both an all-string mapping and a mixed numeric/string mapping.
Add a changelog/ entry.
I'll keep #15010 out of scope since that one is about the Decimal tolerance repr rather than the formatter raising.
Activity
deepak7lal commentedon Sep 13, 2026
I'd like to take this. Reproduced on
main, and the cause is a small asymmetry between the mapping and sequence formatters.ApproxMapping._repr_compare(src/_pytest/approx.py) guards the diff arithmetic with:while
ApproxSequenceLike._repr_compareguards the equivalent block withexcept TypeError, added in #13012 for #13010. Subtracting two strings raisesTypeError, so the mapping path lets it escape into the assertion-repr hook, which is why the details are replaced by the "representation of details failed" message while the sequence equivalent still prints its table:Plan, following #13012:
except (ZeroDivisionError, TypeError)so non-numeric values are skipped for the max-abs/max-rel calculations but still reported in the mismatch table.testing/python/approx.pyalongsidetest_mixed_sequence, covering both an all-string mapping and a mixed numeric/string mapping.changelog/entry.I'll keep #15010 out of scope since that one is about the Decimal tolerance repr rather than the formatter raising.
Fix pytest.approx mapping details for non-numeric values