fix: approx() tolerance not applied to sequences/mappings nested inside dicts - #14498
fix: approx() tolerance not applied to sequences/mappings nested inside dicts#14498Ashutosh-177 wants to merge 6 commits into
Conversation
When approx() is called on a dict whose values are themselves lists or dicts, _approx_scalar() was returning ApproxScalar for those values, which does exact equality. The top-level approx() dispatcher correctly routes sequences to ApproxSequenceLike and mappings to ApproxMapping, but _approx_scalar() -- called per-element by ApproxMapping.__eq__ -- missed those cases. Adding the same isinstance checks in _approx_scalar means nested structures get recursive approximate comparison instead of falling back to ==. Fixes pytest-dev#8703 Signed-off-by: Ashutosh Kumar Singh <ahutoshhjp1067@gmail.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Extends _approx_scalar in python_api.py to dispatch Mapping and sequence-like values to ApproxMapping and ApproxSequenceLike respectively, enabling nested approximate comparisons within scalar dispatch.
Changes:
- Add
Mappingbranch to returnApproxMapping. - Add sequence-like branch to return
ApproxSequenceLike.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hello, thank you for working on pytest @Ashutosh-177, it seems your second commit canceled the first, so there's nothing to review. |
3ada952 to
b613c79
Compare
Signed-off-by: Ashutosh Kumar Singh <ahutoshhjp1067@gmail.com>
Covers the Mapping branch in _approx_scalar() to reach 100% diff coverage. Signed-off-by: Ashutosh Kumar Singh <ahutoshhjp1067@gmail.com>
|
Thanks, but as said in the issue, nested structures are currently not supported, in fact it's a bug that the case you give doesn't throw an error. If we want to support them, a more thorough fix will be needed. If I change your example to fail (e.g. change |
Running into an issue where
pytest.approxworks fine at the top level but silently falls back to exact equality when the same sequence or mapping appears as a dict value.For example:
This fails even though the tolerance should cover the difference. The top-level
approx()call dispatches toApproxMapping, which iterates over the dict values and calls_approx_scalar()on each one. But_approx_scalar()only checks forDecimalanddatetime/timedelta— everything else, including lists and nested dicts, falls through toApproxScalar, which does exact comparison.The top-level
approx()dispatcher already has the correctisinstancechecks forMappingand sequence-like types. Adding the same two checks to_approx_scalar()makes nested structures recurse properly.Fixes #8703