Skip to content

Conversation

@cmnemoi
Copy link
Contributor

@cmnemoi cmnemoi commented Oct 15, 2025

Hello,

This PR fixes an issue with pytest.approx where the error message incorrectly reported all elements as mismatched when comparing mappings with different key orders, even when only some values differed.

The original code paired values by position rather than by key. This caused incorrect mismatch reporting when dictionary keys were in different orders.

This closes #12444.

Checklist

  • I have included new tests or updated existing ones where applicable.
  • I have created a new changelog file in the changelog folder, named <ISSUE NUMBER>.<TYPE>.rst.
  • I have added myself to AUTHORS in alphabetical order.
  • I have added text like “closes #XYZW” to the PR description.
  • I have allowed maintainers to push and squash when merging my commits.
  • I have included documentation when adding new features. (Not applicable here – this is a bug fix.)

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 15, 2025
@cmnemoi
Copy link
Contributor Author

cmnemoi commented Oct 20, 2025

Hello @bluetech, can I have a review here ? It should be fast.

@cmnemoi cmnemoi force-pushed the fix/12444 branch 2 times, most recently from ad30e6c to 99d108b Compare December 2, 2025 14:37
@cmnemoi
Copy link
Contributor Author

cmnemoi commented Dec 2, 2025

Hello @nicoddemus, thanks for your review for #13818

Here is a similar contribution to take a look to when you have the time

Comment on lines +259 to +260
for approx_key, approx_value in approx_side_as_map.items():
other_value = other_side.get(approx_key, None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's access the key directly, because the previous checks already ensure both dicts have the same set of keys.

Suggested change
for approx_key, approx_value in approx_side_as_map.items():
other_value = other_side.get(approx_key, None)
for approx_key, approx_value in approx_side_as_map.items():
other_value = other_side[approx_key]

Comment on lines +1065 to +1074
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the output message is central to the issue, please rewrite this test using pytester and verifying the output itself.

Also, let's use a few more keys in both dictionaries to cover more ground. I suggest:

expected = {"a": 1, "b": 2, "c": 3, "d": 4}
actual = {"d": 4, "c": 5, "a": 8}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytest approx wrong formatting return - printing all values as wrong.

2 participants