Fix pytest.approx mapping details for non-numeric values - #15014
Fix pytest.approx mapping details for non-numeric values#15014deepak7lal wants to merge 1 commit into
Conversation
|
This branch is based on
with decimal.localcontext() as ctx:
ctx.traps[decimal.FloatOperation] = True
assert {"a": Decimal(8), "b": 1.0} == approx({"a": Decimal(9), "b": 9.0})This reports
Rebasing and mirroring the sequence guard fixes #15009 and keeps that path loud: except FloatOperation:
raise
except (ZeroDivisionError, TypeError):
pass |
ApproxMapping._repr_compare guards its diff arithmetic with `except ZeroDivisionError`, so an unequal pair of non-numeric values under the same key raises TypeError into the assertion-repr hook and the mismatch table is replaced by "representation of details failed". The sequence path already handles this. Catching TypeError alone is not enough here. decimal.FloatOperation subclasses it, and pytest-dev#15006 made the sequence path re-raise that ahead of the non-number handler so a mapping mixing Decimals with floats cannot report the smaller of two differences as the maximum. Mirror both clauses rather than only the second. Adds the mapping counterpart of test_mixed_decimal_and_float_sequence_does_not_hide_float_operation, which fails without the re-raise, so the wrong maximum is now caught by the suite. Closes pytest-dev#15009 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
26036af to
01281a1
Compare
|
You are right on every point, and thank you for the detail. Rebased onto I reproduced it before changing anything. Applying the original Reporting The mapping path now mirrors the sequence path rather than copying only its second clause: except FloatOperation:
raise
except (ZeroDivisionError, TypeError):
passOn your other observation, that the suite passes either way: I added the mapping counterpart of
|
Closes #15009.
Problem
ApproxMapping._repr_compareguards its diff arithmetic with:while
ApproxSequenceLike._repr_compareguards the equivalent block withexcept TypeError, added in #13012 for #13010.Subtracting two strings raises
TypeError, so on the mapping path it escapes into the assertion-repr hook and the mismatch table is replaced by a "representation of details failed" message. The same comparison one type over still prints its table:pytest.approxdocuments support for non-numeric mapping values, andtest_dict_nonnumericalready covers them for equality - only the failure-detail formatter is affected.Change
Widen the guard to
except (ZeroDivisionError, TypeError), so non-numeric values are skipped for the max-absolute and max-relative calculations but still reported as mismatches. This mirrors what #13012 did for sequences.Values that compare equal, and mappings that are wholly numeric, are unaffected.
Tests
Two tests in
testing/python/approx.py, next to the existing dict cases:test_mixed_dict- a mapping mixing numbers and strings, mirroringtest_mixed_sequencetest_dict_of_strings- the all-non-numeric mapping from the issue, whereMax absolute/relative differenceremain-infBoth fail on
mainwith theTypeErrorabove.testing/python/approx.pypasses in full (151 tests).Out of scope: #15010, which concerns the
Decimaltolerance repr rather than the formatter raising.