Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error assertion handling in approx when None in dict comparison #11178

Merged
merged 3 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/10702.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed error assertion handling in :func:`pytest.approx` when ``None`` is an expected or received value when comparing dictionaries.
25 changes: 13 additions & 12 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,20 @@
approx_side_as_map.items(), other_side.values()
):
if approx_value != other_value:
max_abs_diff = max(
max_abs_diff, abs(approx_value.expected - other_value)
)
if approx_value.expected == 0.0:
max_rel_diff = math.inf
else:
max_rel_diff = max(
max_rel_diff,
abs(
(approx_value.expected - other_value)
/ approx_value.expected
),
if approx_value.expected is not None and other_value is not None:
max_abs_diff = max(

Check warning on line 269 in src/_pytest/python_api.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python_api.py#L269

Added line #L269 was not covered by tests
max_abs_diff, abs(approx_value.expected - other_value)
)
if approx_value.expected == 0.0:
max_rel_diff = math.inf

Check warning on line 273 in src/_pytest/python_api.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python_api.py#L273

Added line #L273 was not covered by tests
else:
max_rel_diff = max(

Check warning on line 275 in src/_pytest/python_api.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python_api.py#L275

Added line #L275 was not covered by tests
max_rel_diff,
abs(
(approx_value.expected - other_value)
/ approx_value.expected
),
)
different_ids.append(approx_key)

message_data = [
Expand Down
17 changes: 17 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ def test_error_messages_native_dtypes(self, assert_approx_raises_regex):
],
)

assert_approx_raises_regex(
{"a": 1.0, "b": None, "c": None},
{
"a": None,
"b": 1000.0,
"c": None,
},
[
r" comparison failed. Mismatched elements: 2 / 3:",
r" Max absolute difference: -inf",
r" Max relative difference: -inf",
r" Index \| Obtained\s+\| Expected\s+",
rf" a \| {SOME_FLOAT} \| None",
rf" b \| None\s+\| {SOME_FLOAT} ± {SOME_FLOAT}",
],
)

assert_approx_raises_regex(
[1.0, 2.0, 3.0, 4.0],
[1.0, 3.0, 3.0, 5.0],
Expand Down